Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalArgumentException: width and height must be > 0 #6

Open
diegoflassa opened this issue Sep 9, 2024 · 3 comments
Open
Labels
good first issue Good for newcomers

Comments

@diegoflassa
Copy link

I am having this error

                                                                                                java.lang.IllegalArgumentException: width and height must be > 0
                                                                                                	at android.graphics.Bitmap.createBitmap(Bitmap.java:1102)
                                                                                                	at android.graphics.Bitmap.createBitmap(Bitmap.java:941)
                                                                                                	at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:796)
                                                                                                	at com.image.cropview.CropUtil.cropImage(CropUtil.kt:705)
                                                                                                	at com.image.cropview.ImageCrop.onCrop(ImageCropView.kt:161)
                                                                                                	at br.com.havan.logistica.produtoai.ui.home.TelaHomeKt$TelaHome$2$invoke$$inlined$ConstraintLayout$2.invoke(ConstraintLayout.kt:1572)
                                                                                                	at br.com.havan.logistica.produtoai.ui.home.TelaHomeKt$TelaHome$2$invoke$$inlined$ConstraintLayout$2.invoke(ConstraintLayout.kt:89)
                                                                                                	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                	at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:192)
                                                                                                	at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2823)
                                                                                                	at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:3114)
                                                                                                	at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3605)
                                                                                                	at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:3550)
                                                                                                	at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:948)
                                                                                                	at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:1206)
                                                                                                	at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:132)
                                                                                                	at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:616)
                                                                                                	at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:585)
                                                                                                	at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:41)
                                                                                                	at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
                                                                                                	at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
                                                                                                	at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
                                                                                                	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1106)
                                                                                                	at android.view.Choreographer.doCallbacks(Choreographer.java:866)
                                                                                                	at android.view.Choreographer.doFrame(Choreographer.java:792)
                                                                                                	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1092)
                                                                                                	at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                	at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                	at android.os.Looper.loopOnce(Looper.java:226)
                                                                                                	at android.os.Looper.loop(Looper.java:313)
                                                                                                	at android.app.ActivityThread.main(ActivityThread.java:8669)
                                                                                                	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
                                                                                                	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
                                                                                                	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.runtime.PausableMonotonicFrameClock@c0f0b6c, androidx.compose.ui.platform.MotionDurationScaleImpl@96af535, StandaloneCoroutine{Cancelling}@53ebdca, AndroidUiDispatcher@dc68b3b]

When calling your lib, like this:

                val imageCrop: ImageCrop = ImageCrop(bitmap.value!!)

                // Configure ImageCropView.
                imageCrop.ImageCropView(
                    modifier = Modifier.fillMaxSize(),
                    guideLineColor = Color.LightGray,
                    guideLineWidth = 2.dp,
                    edgeCircleSize = 5.dp,
                    showGuideLines = true,
                    cropType = CropType.SQUARE,
                    edgeType = EdgeType.CIRCULAR
                )

                val croppedBitmap = imageCrop.onCrop()

Yes, bitmap.value is a valid bitmap:
bitmap = {ParcelableSnapshotMutableState@33174} Collecting data...
mColorSpace = null
mDensity = 280
mHardwareBuffer = null
mHeight = 3000
mNativePtr = 525779117632
mNinePatchChunk = null
mNinePatchInsets = null
mRecycled = false
mRequestPremultiplied = true
mWidth = 4000
shadow$klass = {Class@18085} "class android.graphics.Bitmap"
shadow$monitor = -1950744464

@diegoflassa
Copy link
Author

diegoflassa commented Sep 9, 2024

This callback is never called

            .onSizeChanged { intSize ->
                cropUtil.onCanvasSizeChanged(intSize = intSize)
            }

And, when this function runs
public fun cropImage(): Bitmap {
The canvas size is 0

@diegoflassa
Copy link
Author

I was able to fix it by modifying the calling function to this:

                val imageCrop: ImageCrop = ImageCrop(bitmap.value!!)

                // Configure ImageCropView.
                imageCrop.ImageCropView(
                    modifier = Modifier.fillMaxSize(),
                    guideLineColor = Color.LightGray,
                    guideLineWidth = 2.dp,
                    edgeCircleSize = 5.dp,
                    showGuideLines = true,
                    cropType = CropType.SQUARE,
                    edgeType = EdgeType.CIRCULAR
                )
                Log.i(tag, "After imageCrop.ImageCropView")
                val coroutineScope = rememberCoroutineScope()
                coroutineScope.launch(Dispatchers.IO) {
                    delay(3000)
                    val croppedBitmap = imageCrop.onCrop()
                }

I am not sure of the ideal value of the delay
Its gives time for the

        .onSizeChanged { intSize ->
            cropUtil.onCanvasSizeChanged(intSize = intSize)
        }

To be called

You should update your example

@rroohit rroohit added the good first issue Good for newcomers label Sep 11, 2024
@rroohit
Copy link
Owner

rroohit commented Sep 13, 2024

Thank you so much for taking the time to identify this issue and report it. Your feedback is incredibly valuable in helping us improve the library. We are already looking into the issue and will have a fix available soon..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants