Last active
October 4, 2019 13:30
-
-
Save NickHolcombe/aafe104a965bc96e8c46bc6d1b643def to your computer and use it in GitHub Desktop.
Barcode blog createBarcodeBitmap.kt
This file contains 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
private fun createBarcodeBitmap( | |
barcodeValue: String, | |
@ColorInt barcodeColor: Int, | |
@ColorInt backgroundColor: Int, | |
widthPixels: Int, | |
heightPixels: Int | |
): Bitmap { | |
val bitMatrix = Code128Writer().encode( | |
barcodeValue, | |
BarcodeFormat.CODE_128, | |
widthPixels, | |
heightPixels | |
) | |
val pixels = IntArray(bitMatrix.width * bitMatrix.height) | |
for (y in 0 until bitMatrix.height) { | |
val offset = y * bitMatrix.width | |
for (x in 0 until bitMatrix.width) { | |
pixels[offset + x] = | |
if (bitMatrix.get(x, y)) barcodeColor else backgroundColor | |
} | |
} | |
val bitmap = Bitmap.createBitmap( | |
bitMatrix.width, | |
bitMatrix.height, | |
Bitmap.Config.ARGB_8888 | |
) | |
bitmap.setPixels( | |
pixels, | |
0, | |
bitMatrix.width, | |
0, | |
0, | |
bitMatrix.width, | |
bitMatrix.height | |
) | |
return bitmap | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment