Created
April 1, 2021 10:46
-
-
Save sdzshn3/f44ccda4437c3fbcd3ae14106eda078b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // QR Code Generator | |
| implementation 'com.google.zxing:core:3.3.3' | |
| private fun generateQr(bookingId: String) { | |
| val size = 300 | |
| val charset = Charsets.UTF_8 | |
| val hintMap = HashMap<EncodeHintType, ErrorCorrectionLevel>() | |
| hintMap[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H | |
| try { | |
| val matrix = MultiFormatWriter().encode( | |
| String(bookingId.toByteArray(), charset), | |
| BarcodeFormat.QR_CODE, | |
| size, size, hintMap | |
| ) | |
| val width = matrix.width | |
| val height = matrix.height | |
| val pixels = IntArray(width * height) | |
| for (y in 0 until height) { | |
| val offset = y * width | |
| for (x in 0 until width) { | |
| pixels[offset + x] = if (matrix.get(x, y)) BLACK else WHITE | |
| } | |
| } | |
| val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | |
| bitmap.setPixels(pixels, 0, width, 0, 0, width, height) | |
| val overlay = BitmapFactory.decodeResource(resources, R.drawable.mounty_png) | |
| //val finalBitmap = mergeBitmaps(overlay, bitmap) | |
| mBinding.qrImage.setImageBitmap(bitmap) | |
| } catch (e: Exception) { | |
| e.printStackTrace() | |
| } | |
| } | |
| private fun mergeBitmaps(overlay: Bitmap, bitmap: Bitmap): Bitmap { | |
| val height = bitmap.height | |
| val width = bitmap.width | |
| val combined = Bitmap.createBitmap(width, height, bitmap.config) | |
| val canvas = Canvas(combined) | |
| val canvasWidth = canvas.width | |
| val canvasHeight = canvas.height | |
| canvas.drawBitmap(bitmap, Matrix(), null) | |
| val centreX = (canvasWidth - overlay.width) / 2 | |
| val centreY = (canvasHeight - overlay.height) / 2 | |
| canvas.drawBitmap(overlay, centreX.toFloat(), centreY.toFloat(), null) | |
| return combined | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment