Skip to content

Instantly share code, notes, and snippets.

@ilhamsuaib
Created September 25, 2018 06:01
Show Gist options
  • Save ilhamsuaib/58c0b523899bd78865ce6691ef0b71fc to your computer and use it in GitHub Desktop.
Save ilhamsuaib/58c0b523899bd78865ce6691ef0b71fc to your computer and use it in GitHub Desktop.
encode image to bitmap black and transparent
import android.graphics.Bitmap
import com.google.zxing.BarcodeFormat
import com.google.zxing.MultiFormatWriter
import com.google.zxing.common.BitMatrix
/**
* Created by ilham on 9/19/17.
*/
class ImageEncoder {
companion object {
fun createBitmapImage(id: String?): Bitmap {
val formatWriter = MultiFormatWriter()
val bitMatrix = formatWriter.encode(id, BarcodeFormat.QR_CODE, 280, 280)
fun createBitmap(matrix: BitMatrix): Bitmap {
val transparent = 0x00FFFFFF
val black = -0x1000000
val height = matrix.height
val width = matrix.width
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
for (x in 0 until width) {
for (y in 0 until height) {
bmp.setPixel(x, y, if (matrix.get(x, y)) black else transparent)
}
}
return bmp
}
return createBitmap(bitMatrix)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment