如何在手機(jī)上生成黑白圖傳讓電子墨水屏顯示出來?
一開始我在網(wǎng)上找了一個轉(zhuǎn)黑白圖片的方法,這個方法的邏輯是 用127做臨界值抄邀,來判斷灰度圖的灰度是否比他大,大就打白昼榛,小就打黑境肾,代碼和效果圖如下
/**
* 轉(zhuǎn)為二值圖像
*
* @param bmp 原圖bitmap
*
* @return
*/
private fun convertToBMW(bmp: Bitmap, tmp:Int=130): Bitmap {
val width = bmp.width // 獲取位圖的寬
val height = bmp.height // 獲取位圖的高
val pixels = IntArray(width * height) // 通過位圖的大小創(chuàng)建像素點(diǎn)數(shù)組
// 設(shè)定二值化的域值,默認(rèn)值為100
//tmp = 180;
bmp.getPixels(pixels, 0, width, 0, 0, width, height)
var alpha = 0xFF shl 24
for (i in 0 until height) {
for (j in 0 until width) {
val grey = pixels[width * i + j]
// 分離三原色
alpha = grey and -0x1000000 shr 24
var red = grey and 0x00FF0000 shr 16
var green = grey and 0x0000FF00 shr 8
var blue = grey and 0x000000FF
if (red > tmp) {
red = 255
} else {
red = 0
}
if (blue > tmp) {
blue = 255
} else {
blue = 0
}
if (green > tmp) {
green = 255
} else {
green = 0
}
pixels[width * i + j] = (alpha shl 24 or (red shl 16) or (green shl 8)
or blue)
if (pixels[width * i + j] == -1) {
pixels[width * i + j] = -1
} else {
pixels[width * i + j] = -16777216
}
}
}
// 新建圖片
val newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
// 設(shè)置圖片數(shù)據(jù)
newBmp.setPixels(pixels, 0, width, 0, 0, width, height)
val bitmap = ThumbnailUtils.extractThumbnail(newBmp, width, height)
imageView5.setImageBitmap(bitmap)
return bitmap
}
黑白效果圖
這種方式有一個問題胆屿,要么一大片白色要么一大片黑色奥喻,效果不是很理想。如果打印出來根本認(rèn)不出來這是誰非迹。
后面了解到Floyd-Steinberg算法环鲤,它是利用誤差的擴(kuò)散算法的Floyd-Steinberg抖動算法來對圖像進(jìn)行二值化處理。
例如憎兽,灰度如的灰度值為g冷离,誤差值為e。遍歷每個像素值纯命,灰度如果大于m(127西剥,或者像素灰度平均值,看你喜歡)亿汞,那么pixels【i】=#ffffffff瞭空,打白,e=g-255留夜;否則,打黑图甜,pixels【i】=#ff000000碍粥,e=g;然后黑毅,這個像素點(diǎn)的右邊嚼摩,下邊,和右下方的像素點(diǎn)矿瘦,對應(yīng)的加上3e/8,3e/8,e/4枕面。最后你的到的像素數(shù)組在轉(zhuǎn)成bitmap,就是抖動的單色圖了缚去。效果圖如下
//抖動算法來對圖像進(jìn)行二值化處理
private fun convertGreyImgByFloyd(img: Bitmap): Bitmap {
val width = img.width //獲取位圖的寬
val height = img.height //獲取位圖的高
val pixels = IntArray(width * height) //通過位圖的大小創(chuàng)建像素點(diǎn)數(shù)組
img.getPixels(pixels, 0, width, 0, 0, width, height)
val gray = IntArray(height * width)
for (i in 0 until height) {
for (j in 0 until width) {
val grey = pixels[width * i + j]
val red = grey and 0x00FF0000 shr 16
gray[width * i + j] = red
}
}
var e = 0
for (i in 0 until height) {
for (j in 0 until width) {
val g = gray[width * i + j]
if (g >= 128) {
pixels[width * i + j] = -0x1
e = g - 255
} else {
pixels[width * i + j] = -0x1000000
e = g - 0
}
if (j < width - 1 && i < height - 1) {
//右邊像素處理
gray[width * i + j + 1] += 3 * e / 8
//下
gray[width * (i + 1) + j] += 3 * e / 8
//右下
gray[width * (i + 1) + j + 1] += e / 4
} else if (j == width - 1 && i < height - 1) {//靠右或靠下邊的像素的情況
//下方像素處理
gray[width * (i + 1) + j] += 3 * e / 8
} else if (j < width - 1 && i == height - 1) {
//右邊像素處理
gray[width * i + j + 1] += e / 4
}
}
}
val mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
mBitmap.setPixels(pixels, 0, width, 0, 0, width, height)
imageView5.setImageBitmap(mBitmap)
saveBmp(mBitmap)
return mBitmap
}
效果圖
現(xiàn)在的效果達(dá)到了要求潮秘,后面可以把它轉(zhuǎn)成單色圖,通過傳每一個像素給藍(lán)牙設(shè)備顯示出來易结。
轉(zhuǎn)成bmp圖片枕荞,保存到手機(jī)
/**
* 將Bitmap存為 .bmp格式圖片
* @param bitmap
*/
private fun saveBmp(bitmap: Bitmap?) {
if (bitmap == null)
return
// 位圖大小
val nBmpWidth = bitmap.width
val nBmpHeight = bitmap.height
// 圖像數(shù)據(jù)大小
val bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4)
try {
// 存儲文件名
val filename = "/sdcard/test${Random.nextInt(1000)}.bmp"
val file = File(filename)
if (!file.exists()) {
file.createNewFile()
}
val fileos = FileOutputStream(filename)
// bmp文件頭
val bfType = 0x4d42
val bfSize = (14 + 40 + bufferSize).toLong()
val bfReserved1 = 0
val bfReserved2 = 0
val bfOffBits = (14 + 40).toLong()
// 保存bmp文件頭
writeWord(fileos, bfType)
writeDword(fileos, bfSize)
writeWord(fileos, bfReserved1)
writeWord(fileos, bfReserved2)
writeDword(fileos, bfOffBits)
// bmp信息頭
val biSize = 40L
val biWidth = nBmpWidth.toLong()
val biHeight = nBmpHeight.toLong()
val biPlanes = 1
val biBitCount = 24
val biCompression = 0L
val biSizeImage = 0L
val biXpelsPerMeter = 0L
val biYPelsPerMeter = 0L
val biClrUsed = 0L
val biClrImportant = 0L
// 保存bmp信息頭
writeDword(fileos, biSize)
writeLong(fileos, biWidth)
writeLong(fileos, biHeight)
writeWord(fileos, biPlanes)
writeWord(fileos, biBitCount)
writeDword(fileos, biCompression)
writeDword(fileos, biSizeImage)
writeLong(fileos, biXpelsPerMeter)
writeLong(fileos, biYPelsPerMeter)
writeDword(fileos, biClrUsed)
writeDword(fileos, biClrImportant)
// 像素掃描
val bmpData = ByteArray(bufferSize)
val wWidth = nBmpWidth * 3 + nBmpWidth % 4
var nCol = 0
var nRealCol = nBmpHeight - 1
while (nCol < nBmpHeight) {
run {
var wRow = 0
var wByteIdex = 0
while (wRow < nBmpWidth) {
val clr = bitmap.getPixel(wRow, nCol)
bmpData[nRealCol * wWidth + wByteIdex] = Color.blue(clr).toByte()
bmpData[nRealCol * wWidth + wByteIdex + 1] = Color.green(clr).toByte()
bmpData[nRealCol * wWidth + wByteIdex + 2] = Color.red(clr).toByte()
wRow++
wByteIdex += 3
}
}
++nCol
--nRealCol
}
fileos.write(bmpData)
fileos.flush()
fileos.close()
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
}
@Throws(IOException::class)
protected fun writeWord(stream: FileOutputStream, value: Int) {
val b = ByteArray(2)
b[0] = (value and 0xff).toByte()
b[1] = (value shr 8 and 0xff).toByte()
stream.write(b)
}
@Throws(IOException::class)
protected fun writeDword(stream: FileOutputStream, value: Long) {
val b = ByteArray(4)
b[0] = (value and 0xff).toByte()
b[1] = (value shr 8 and 0xff).toByte()
b[2] = (value shr 16 and 0xff).toByte()
b[3] = (value shr 24 and 0xff).toByte()
stream.write(b)
}
@Throws(IOException::class)
protected fun writeLong(stream: FileOutputStream, value: Long) {
val b = ByteArray(4)
b[0] = (value and 0xff).toByte()
b[1] = (value shr 8 and 0xff).toByte()
b[2] = (value shr 16 and 0xff).toByte()
b[3] = (value shr 24 and 0xff).toByte()
stream.write(b)
}
End柜候!