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

overlay image on qrCode #51

Closed chuongdk2011 closed 6 months ago

chuongdk2011 commented 6 months ago

Can the library support doing something like this?:

image image

alexzhirkevich commented 6 months ago

You can do

val options = createQrVectorOptions {
    colors {
        dark = QrVectorColor.Eraser
    }
    background {
        color = QrVectorColor.Solid(Color.WHITE)
    }
}

This will create drawable where QRcode is cut from white background. Then you can place this drawable above any other View and this view will be visible throught the holes

Or you can merge your image and this drawable using

val resultDrawable  = LayerDrawable(arrayOf(imageDrawable, qrCodeDrawable))
alexzhirkevich commented 6 months ago

Or you can use this to just cut QR code from your provided imageDrawable

val options = createQrVectorOptions {
    colors {
        light = QrVectorColor.Eraser
        dark = QrVectorColor.Transparent
    }
    background {
        drawable = imageDrawable
    }
}
chuongdk2011 commented 6 months ago

I tried the above codes, but my idea is to overlay the image on the places where the QrCode is, like set a color for the QrCode, and the remaining parts that don't have the Qrcode will show up in the default background color.

chuongdk2011 commented 6 months ago

It seems your code above is doing the opposite.

alexzhirkevich commented 6 months ago

You can create custom QrVectorColor and make a shaderPaint using BitmapShader from your image

alexzhirkevich commented 6 months ago

Here is an example:

class QrImageColor(private val image : Drawable) : QrVectorColor {
    override fun createPaint(width: Float, height: Float): Paint {
        val bitmap = image.toBitmap(width.roundToInt(),height.roundToInt())
        val bitmapShader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        return Paint().apply {
            shader = bitmapShader
        }
    }
}
alexzhirkevich commented 6 months ago

@chuongdk2011 Doest it fit your needs?

chuongdk2011 commented 6 months ago

Yes, I did it. Thank you very much