alexzhirkevich/custom-qr-generator

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

Opened this issue · 6 comments

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

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

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();
   }

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)

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

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

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();
   }