Created
April 25, 2022 14:33
-
-
Save samuel22gj/290559e3f41e8f4683f43dbde52dd9a4 to your computer and use it in GitHub Desktop.
Generate QR Code by ZXing in Android
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
import android.graphics.Bitmap | |
import com.google.zxing.BarcodeFormat | |
import com.google.zxing.EncodeHintType | |
import com.google.zxing.MultiFormatWriter | |
import com.google.zxing.common.BitMatrix | |
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel | |
object BarcodeEncoder { | |
fun createBitmap( | |
matrix: BitMatrix, | |
bgColor: Int = 0xFFFFFFFF.toInt(), | |
fgColor: Int = 0xFF000000.toInt(), | |
): Bitmap { | |
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)) fgColor else bgColor | |
} | |
} | |
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | |
bitmap.setPixels(pixels, 0, width, 0, 0, width, height) | |
return bitmap | |
} | |
fun encode( | |
contents: String, | |
format: BarcodeFormat, | |
width: Int, | |
height: Int = width, | |
hints: Map<EncodeHintType, Any>? = null, | |
): BitMatrix { | |
return MultiFormatWriter().encode(contents, format, width, height, hints) | |
} | |
fun encodeQrcode( | |
contents: String, | |
width: Int, | |
height: Int = width, | |
errorCorrection: ErrorCorrectionLevel? = null, // default is Low | |
margin: Int? = null // default is 4 | |
): BitMatrix { | |
return encode( | |
contents, | |
BarcodeFormat.QR_CODE, | |
width, | |
height, | |
buildMap { | |
if (errorCorrection != null) put(EncodeHintType.ERROR_CORRECTION, errorCorrection) | |
if (margin != null) put(EncodeHintType.MARGIN, margin) | |
} | |
) | |
} | |
fun encodeAsBitmap( | |
contents: String, | |
format: BarcodeFormat, | |
width: Int, | |
height: Int = width, | |
hints: Map<EncodeHintType, Any>? = null, | |
bgColor: Int = 0xFFFFFFFF.toInt(), | |
fgColor: Int = 0xFF000000.toInt(), | |
): Bitmap { | |
return createBitmap(encode(contents, format, width, height, hints), bgColor, fgColor) | |
} | |
fun encodeQrcodeAsBitmap( | |
contents: String, | |
width: Int, | |
height: Int = width, | |
errorCorrection: ErrorCorrectionLevel? = null, | |
margin: Int? = null, | |
bgColor: Int = 0xFFFFFFFF.toInt(), | |
fgColor: Int = 0xFF000000.toInt(), | |
): Bitmap { | |
return createBitmap( | |
encodeQrcode(contents, width, height, errorCorrection, margin), bgColor, fgColor | |
) | |
} | |
} |
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
dependencies { | |
implementation "com.google.zxing:core:3.4.1" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment