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 #31

Open
hardikbhalodi opened this issue Jun 26, 2023 · 6 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@hardikbhalodi
Copy link

Describe the bug
at android.graphics.Bitmap.createBitmap (Bitmap.java:1111)
at android.graphics.Bitmap.createBitmap (Bitmap.java:1078)
at android.graphics.Bitmap.createBitmap (Bitmap.java:1028)
at android.graphics.Bitmap.createBitmap (Bitmap.java:989)
at androidx.core.graphics.drawable.DrawableKt.toBitmap (Drawable.kt:68)
at com.github.alexzhirkevich.customqrgenerator.style.BitmapScale$FitXY.scale (BitmapScale.kt:24)
at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.createLogo (QrCodeDrawable.kt:522)
at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.resize (QrCodeDrawable.kt:734)
at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.setBounds (QrCodeDrawable.kt:159)

Versions
Library : 1.6.2'
Android: 12,13

@hardikbhalodi hardikbhalodi added the bug Something isn't working label Jun 26, 2023
@alexzhirkevich
Copy link
Owner

You probably trying to create logo with zero size. Anyway i need a code snippet to reproduce

@hardikbhalodi
Copy link
Author

hardikbhalodi commented Jun 27, 2023

The error from production app.
Here is my code but i could not reproduce it!.


 public QrVectorLogo addLogo(final Context context, final String logoPath){
Drawable drawable = buildDrawableFromPath(context,logoPath);
     
             float size = .16f;
            if (drawable != null) {
                QrVectorLogoShape shape = QrVectorLogoShape.Circle.INSTANCE;

           QrVectorLogo.Builder builder =
                        new QrVectorLogo.Builder().size(size).drawable(drawable).shape(shape);
                builder.setBackgroundColor(getQRVectorColor(Color.parseColor("#FFFFFF")));
                return builder.build();
            }
            return new QrVectorLogo.Builder().build();
   }

@hardikbhalodi
Copy link
Author

hardikbhalodi commented Aug 9, 2023

Again got the same issue in following models

Android 13
Model: Galaxy F62

Android 12
Model: Galaxy M21, Moto G31

Android 11
Model: Galaxy A30s, Redmi Note 10T 5G

Library version:
'com.github.alexzhirkevich:custom-qr-generator:1.6.2'

Code Snippet

 public QrVectorLogo addLogo(final Context context, final String logoPath){
Drawable drawable = buildDrawableFromPath(context,logoPath);
     
             float size = .16f;
            if (drawable != null) {
                QrVectorLogoShape shape = QrVectorLogoShape.Circle.INSTANCE;

           QrVectorLogo.Builder builder =
                        new QrVectorLogo.Builder().size(size).drawable(drawable).shape(shape);
                builder.setBackgroundColor(getQRVectorColor(Color.parseColor("#FFFFFF")));
                return builder.build();
            }
            return new QrVectorLogo.Builder().build();
   }

Exception caught

Fatal Exception: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:1118)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1085)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1035)
at android.graphics.Bitmap.createBitmap(Bitmap.java:996)
at androidx.core.graphics.drawable.DrawableKt.toBitmap(Drawable.kt:68) <=== Seems problem is here
at com.github.alexzhirkevich.customqrgenerator.style.BitmapScale$FitXY.scale(BitmapScale.java:24)
at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.createLogo(QrCodeDrawable.kt:522)
at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.resize(QrCodeDrawable.kt:734)
at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.setBounds(QrCodeDrawable.kt:159)

@hardikbhalodi
Copy link
Author

hardikbhalodi commented Aug 10, 2023

val logoSize = size * options.logo.size <== **here logoSize can be 0?**

    val logoBgSize = (logoSize * (1 + options.logo.padding.value)).roundToInt()
    if (options.logo.padding is QrVectorLogoPadding.Natural) {
        applyNaturalLogo(logoBgSize, size, pixelSize)
    }

    val logoBackgroundPath =
        options.logo.shape.createPath(logoBgSize.toFloat(), Neighbors.Empty)

    val logoPaint = when {
        options.logo.padding is QrVectorLogoPadding.Empty -> null
        options.logo.backgroundColor is QrVectorColor.Unspecified -> options.background.color
        else -> options.logo.backgroundColor
    }?.createPaint(logoBgSize.toFloat(), logoBgSize.toFloat())

    createMainElements(pixelSize, framePath, ballPath, darkPixelPath, lightPixelPath, darkTimingPath, lightTimingPath)

    val logo = createLogo(logoSize)  <= and zero size will be passed through this parameter without checking

@IbrahimSayed94
Copy link

Put a default size

private const val DEFAULT_SIZE = 400

Because if you're dealing with a Drawable generated dynamically and not from a resource, obtaining intrinsic dimensions might not be straightforward. In such cases, we can use a default size or set a reasonable size for the bitmap if intrinsic dimensions are not available

@hardikbhalodi
Copy link
Author

hardikbhalodi commented Feb 5, 2024

Yes but that code from SDK, so @alexzhirkevich can fix it.

This is my code

 public QrVectorLogo addLogo(final Context context, final String logoPath){
Drawable drawable = buildDrawableFromPath(context,logoPath);
     
             float size = .16f; <= Giving size for logo
            if (drawable != null) {
                QrVectorLogoShape shape = QrVectorLogoShape.Circle.INSTANCE;

           QrVectorLogo.Builder builder =
                        new QrVectorLogo.Builder().size(size).drawable(drawable).shape(shape);
                builder.setBackgroundColor(getQRVectorColor(Color.parseColor("#FFFFFF")));
                return builder.build();
            }
            return new QrVectorLogo.Builder().build();
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants