Created
September 25, 2018 06:01
-
-
Save ilhamsuaib/58c0b523899bd78865ce6691ef0b71fc to your computer and use it in GitHub Desktop.
encode image to bitmap black and transparent
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.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