Skip to content

Instantly share code, notes, and snippets.

@NickHolcombe
Last active October 4, 2019 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickHolcombe/aafe104a965bc96e8c46bc6d1b643def to your computer and use it in GitHub Desktop.
Save NickHolcombe/aafe104a965bc96e8c46bc6d1b643def to your computer and use it in GitHub Desktop.
Barcode blog createBarcodeBitmap.kt
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