alexzhirkevich / custom-qr-generator

Android library for creating QR codes with logo, custom shapes, colors, background image. Powered by ZXing
MIT License
173 stars 18 forks source link

Error if set logo #10

Closed BadKiko closed 1 year ago

BadKiko commented 1 year ago

If i set logo from DrawableSource.Resource it crash my app, if i set DrawableSource.Empty all ok I convert drawable to bitmap and it throw me error if i set logo Logs: java.lang.IllegalArgumentException: width and height must be > 0 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:33) at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.resize(QrCodeDrawableImpl.kt:369) at com.github.alexzhirkevich.customqrgenerator.vector.QrCodeDrawableImpl.setBounds(QrCodeDrawableImpl.kt:128) at androidx.core.graphics.drawable.DrawableKt.toBitmap(Drawable.kt:72) at androidx.core.graphics.drawable.DrawableKt.toBitmap$default(Drawable.kt:46)

Code(Jetpack Compose used): val data = QrData.Text(curSchedule?.JoinCode.toString()) val options = QrVectorOptions.Builder() .setPadding(.3f) .setErrorCorrectionLevel(QrErrorCorrectionLevel.High) .setLogo( QrVectorLogo( drawable =drawable, ) ) .setShapes( QrVectorShapes( darkPixel = QrVectorPixelShape .RoundCorners(.5f), ball = QrVectorBallShape .RoundCorners(.25f), frame = QrVectorFrameShape .RoundCorners(.25f), ) ) .build() val drawable = QrCodeDrawable(context, data, options) Image( bitmap = drawable.toBitmap(1024, 1024).asImageBitmap(), contentDescription = "qrCode", modifier = Modifier .fillMaxSize() )

alexzhirkevich commented 1 year ago

Try this accompanist library to convert drawable to compose painter

BadKiko commented 1 year ago

I try it before write this issue it doesn't work, but i doesnt test it without logo

alexzhirkevich commented 1 year ago

Can you share the image you use for a logo?

BadKiko commented 1 year ago

Cant find svg, xml:

BadKiko commented 1 year ago

<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="1000dp" android:height="1000dp" android:viewportWidth="1000" android:viewportHeight="1000"> <path android:pathData="M293.11,553.42h-7.36c-4.49,0 -7.32,-2.16 -8.5,-6.47l-20.31,-73.99c-0.68,-2.62 -0.42,-4.7 0.76,-6.22c1.18,-1.52 3.13,-2.28 5.84,-2.28h8.76c4.48,0 7.28,2.2 8.38,6.6l11.68,46.32l12.18,-46.45c1.18,-4.31 3.98,-6.47 8.38,-6.47h7.23c4.48,0 7.32,2.16 8.5,6.47l12.44,45.82l11.93,-45.69c1.1,-4.4 3.89,-6.6 8.38,-6.6h3.55c2.71,0 4.63,0.76 5.77,2.28c1.14,1.52 1.37,3.6 0.7,6.22l-20.56,73.99c-1.1,4.32 -3.93,6.47 -8.5,6.47h-7.61c-4.57,0 -7.4,-2.16 -8.5,-6.47l-12.44,-45.69l-12.18,45.69C300.51,551.26 297.68,553.42 293.11,553.42z" android:fillColor="#FFFFFF"/> <path android:pathData="M400.35,553.42h-9.52c-5.08,0 -7.61,-2.54 -7.61,-7.61V472.2c0,-5.08 2.54,-7.61 7.61,-7.61h9.52c5.08,0 7.61,2.54 7.61,7.61v73.61C407.96,550.88 405.43,553.42 400.35,553.42z" android:fillColor="#FFFFFF"/> <path android:pathData="M458.85,553.42h-25.76c-5.08,0 -7.61,-2.54 -7.61,-7.61V472.2c0,-5.08 2.54,-7.61 7.61,-7.61h25.76c10.24,0 18.57,2.07 25,6.22c5.84,3.81 10.32,8.99 13.45,15.55c3.13,6.56 4.7,13.98 4.7,22.27c0,18.11 -5.88,31.01 -17.64,38.71C477.85,551.39 469.34,553.42 458.85,553.42zM450.22,482.6v53.18h8.63c6.09,0 10.28,-1.52 12.56,-4.57c3.13,-3.98 4.7,-11.51 4.7,-22.59c0,-10.49 -1.74,-17.85 -5.2,-22.08c-2.37,-2.62 -6.39,-3.93 -12.06,-3.93H450.22z" android:fillColor="#FFFFFF"/> <path android:pathData="M551.75,554.94c-13.12,0 -23.18,-4.14 -30.2,-12.44c-6.94,-8.29 -10.41,-19.59 -10.41,-33.89c0,-13.11 3.68,-23.99 11.04,-32.62c7.53,-8.63 17.39,-12.95 29.57,-12.95c7.61,0 14.4,1.78 20.37,5.33c5.97,3.55 10.43,8.17 13.39,13.83c1.27,2.46 1.27,4.49 0,6.09c-1.27,1.61 -3.34,2.41 -6.22,2.41h-9.14c-1.69,0 -3.05,-0.34 -4.06,-1.02c-1.01,-0.68 -2.28,-1.99 -3.81,-3.93l-0.63,-0.76c-2.37,-2.62 -5.67,-3.93 -9.9,-3.93c-9.82,0 -14.72,9.18 -14.72,27.54c0,8.12 0.84,14.72 2.54,19.8c2.11,5.92 6.18,8.88 12.18,8.88c6.77,0 12.1,-1.9 15.99,-5.71v-9.26h-8.38c-4.57,0 -7.32,-2.24 -8.25,-6.73l-0.76,-3.43c-0.51,-2.62 -0.17,-4.67 1.02,-6.16c1.18,-1.48 3.13,-2.22 5.84,-2.22h24.75c5.08,0 7.62,2.54 7.62,7.61v21.19c0,3.72 -0.63,6.3 -1.9,7.74c-3.72,4.4 -8.72,7.93 -14.98,10.6C566.43,553.61 559.45,554.94 551.75,554.94z" android:fillColor="#FFFFFF"/> <path android:pathData="M661.28,553.42h-50.76c-5.08,0 -7.61,-2.54 -7.61,-7.61V472.2c0,-5.08 2.54,-7.61 7.61,-7.61h48.86c2.62,0 4.53,0.74 5.71,2.22c1.18,1.48 1.52,3.53 1.02,6.16l-0.63,3.3c-0.93,4.49 -3.72,6.73 -8.38,6.73h-29.44v15.99h17.89c5.08,0 7.61,2.54 7.61,7.61v3.43c0,5.08 -2.54,7.61 -7.61,7.61h-17.89v17.26h33.63c5.08,0 7.61,2.54 7.61,7.61v3.3C668.89,550.88 666.35,553.42 661.28,553.42z" android:fillColor="#FFFFFF"/> <path android:pathData="M712.55,553.42h-9.52c-5.08,0 -7.61,-2.54 -7.61,-7.61v-63.58h-16.37c-5.08,0 -7.61,-2.54 -7.61,-7.61v-2.41c0,-5.08 2.54,-7.61 7.61,-7.61h57.36c2.71,0 4.65,0.74 5.84,2.22c1.18,1.48 1.48,3.53 0.89,6.16l-0.51,2.54c-1.02,4.49 -3.81,6.73 -8.38,6.73h-14.09v63.58C720.16,550.88 717.62,553.42 712.55,553.42z" android:fillColor="#FFFFFF"/> <path android:pathData="M415.82,353.71c0,-16.51 -4.21,-28.99 -12.63,-37.44c-8.42,-8.44 -19.73,-12.67 -33.94,-12.67c-13.66,0 -24.83,4.32 -33.53,12.94c-8.7,8.62 -13.04,20.99 -13.04,37.11v84.78h93.14V353.71z" android:fillColor="#FFFFFF"/> <path android:pathData="M677.32,353.65c0,-16.49 -4.21,-28.95 -12.63,-37.39c-8.42,-8.44 -19.73,-12.66 -33.94,-12.66c-13.84,0 -25.06,4.32 -33.66,12.95c-8.6,8.64 -12.91,21.02 -12.91,37.16v84.72h93.14V353.65z" android:fillColor="#FFFFFF"/> <path android:pathData="M559.77,617.53c-16.28,15.21 -36.29,22.8 -60.05,22.8c-24.13,0 -43.87,-7.32 -59.21,-21.96c-11.47,-10.99 -18.94,-24.57 -22.42,-40.75h-94.26c4.3,43.22 20.65,78.58 49.06,106.1c31.98,31.12 74.25,46.67 126.82,46.67c52.93,0 95.76,-15.83 128.49,-47.51c27.93,-27.03 43.94,-62.12 48.04,-105.26h-94.34C578.47,593.65 571.09,606.95 559.77,617.53z" android:fillColor="#FFFFFF"/> </vector>

BadKiko commented 1 year ago

doesnt work with rememberDrawablePainter, image is empty in card

code:

@Composable fun ShowQRJoinCode() { val context = LocalContext.current val data = QrData.Text(curSchedule?.JoinCode.toString()) val options = QrVectorOptions.Builder() .setErrorCorrectionLevel(QrErrorCorrectionLevel.High) .setColors( QrVectorColors( dark = QrVectorColor.Solid(MaterialTheme.colors.secondary.toStandardColor()), ball = QrVectorColor.Solid(MaterialTheme.colors.secondary.toStandardColor()), frame = QrVectorColor.Solid(MaterialTheme.colors.primary.toStandardColor()) ) ) .setBackground(QrVectorBackground()) .setShapes( QrVectorShapes( darkPixel = QrVectorPixelShape .RoundCorners(.5f), ball = QrVectorBallShape.Circle(1f), frame = QrVectorFrameShape .RoundCorners(.25f), ) ) .build() val qrDrawable = QrCodeDrawable(context, data, options) Card( shape = RoundedCornerShape(15.dp), contentColor = MaterialTheme.colors.background, backgroundColor = MaterialTheme.colors.background, border = BorderStroke(1.dp, MaterialTheme.colors.primary), modifier = Modifier.padding(horizontal = 30.dp, vertical = 0.dp) ) { Column() { Image( painter = rememberDrawablePainter(drawable = qrDrawable), contentDescription = "qrCode", modifier = Modifier.padding(30.dp) ) } } }

alexzhirkevich commented 1 year ago

doesnt work with rememberDrawablePainter, image is empty in card

code:

You didn't set up a logo in your last code example


Works as expected with rememberDrawablePainter.

val code = QrCodeDrawable(
    context,
    { "Hello world" },
    createQrVectorOptions {
        background {
            color = QrVectorColor.Solid(Color.GRAY)
        }
        errorCorrectionLevel = QrErrorCorrectionLevel.High
        logo {
            size = .25f
            shape = QrVectorLogoShape.Circle
            padding = QrVectorLogoPadding.Natural(.05f)
            drawable = DrawableSource.Resource(R.drawable.img)
        }
    }
)
Box(modifier = Modifier.fillMaxSize()) {
    Image(
        modifier = Modifier.size(300.dp)
            .align(Alignment.Center),
        painter = rememberDrawablePainter (code),
        contentDescription = ""
    )
}

Your logo is white and invisible on white background

alexzhirkevich commented 1 year ago

If you still have issues with rememberDrawablePainter you can use AndroidView


AndroidView(
  factory = {
      ImageView(it)
  },
  update = {
      it.setImageDrawable(code)
  }
)
alexzhirkevich commented 1 year ago

From 1.5.5 Drawable QR codes can be safely converted to Bitmap and used as Compose ImageBitmap