最近有一个需求,需要在手机上生成黑白图传给蓝牙设备显示出来。

一开始我在网上找了一个转黑白图片的方法,这个方法的逻辑是 用127做临界值,来判断灰度图的灰度是否比他大,大就打白,小就打黑,代码和效果图如下

/**

* 转为二值图像

*

* @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) // 通过位图的大小创建像素点数组

// 设定二值化的域值,默认值为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)

// 设置图片数据

newBmp.setPixels(pixels, 0, width, 0, 0, width, height)

val bitmap = ThumbnailUtils.extractThumbnail(newBmp, width, height)

imageView5.setImageBitmap(bitmap)

return bitmap

}

黑白效果图

这种方式有一个问题,要么一大片白色要么一大片黑色,效果不是很理想。如果打印出来根本认不出来这是谁。

后面了解到Floyd-Steinberg算法,它是利用误差的扩散算法的Floyd-Steinberg抖动算法来对图像进行二值化处理。

例如,灰度如的灰度值为g,误差值为e。遍历每个像素值,灰度如果大于m(127,或者像素灰度平均值,看你喜欢),那么pixels【i】=#ffffffff,打白,e=g-255;否则,打黑,pixels【i】=#ff000000,e=g;然后,这个像素点的右边,下边,和右下方的像素点,对应的加上3e/8,3e/8,e/4。最后你的到的像素数组在转成bitmap,就是抖动的单色图了。效果图如下

//抖动算法来对图像进行二值化处理

private fun convertGreyImgByFloyd(img: Bitmap): Bitmap {

val width = img.width //获取位图的宽

val height = img.height //获取位图的高

val pixels = IntArray(width * height) //通过位图的大小创建像素点数组

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

}

效果图

现在的效果达到了要求,后面可以把它转成单色图,通过传每一个像素给蓝牙设备显示出来。

转成bmp图片,保存到手机

/**

* 将Bitmap存为 .bmp格式图片

* @param bitmap

*/

private fun saveBmp(bitmap: Bitmap?) {

if (bitmap == null)

return

// 位图大小

val nBmpWidth = bitmap.width

val nBmpHeight = bitmap.height

// 图像数据大小

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!

android将彩图转为黑白_android 彩色图片二值化转可打印的点阵黑白图相关推荐

  1. 一张彩色图片,如何用Photoshop处理成一张轮廓图(就是变成刚用铅笔画出来时的那样)_......

    一张彩色图片,如何用Photoshop处理成一张轮廓图(就是变成刚用铅笔画出来时的那样)_... 1.在Photoshop中打开一张人物照片,按下快捷键"Ctrl+Shift+U" ...

  2. 【pytorch】yolov4 实现对蛾子数据集的识别 以及 对蛾子图片二值化处理 实现 自动打标签标注。(本文重点在自动实现将图片转化为voc数据集)

    文章目录 一.之前工作回顾 二.图片处理过程 (1)图片的裁剪 (2)生成xml文件的相关代码 三.使用yolov4模型,训练预测蛾子数据集 四.项目步骤记录 五.数据标注范例 六.关于xml的创建. ...

  3. python图像灰度化_python实现图片二值化及灰度处理方式

    python实现图片二值化及灰度处理方式 我就废话不多说了,直接上代码吧! 集成环境:win10 pycharm #!/usr/bin/env python3.5.2 # -*- coding: ut ...

  4. python灰度处理打印图片_python实现图片二值化及灰度处理方式

    我就废话不多说了,直接上代码吧! 集成环境:win10 pycharm #!/usr/bin/env python3.5.2 # -*- coding: utf-8 -*- '''4图片灰度调整及二值 ...

  5. android将彩图转为黑白_彩图转黑白的12种方法

    前几天收拾书架,翻到了几年前上软件课的笔记,其中有一页潦草记了把彩图转成黑白的12种方法.没错,就是辣么多! 一张彩图转为黑白很简单,为什么还要用这么多种方法 因为不同的操作,转出来的图片区别是很大的 ...

  6. android将彩图转为黑白_如何快速修出高质感黑白照片

    作者:曼哈顿印象 微博:曼哈顿印象PHOTO 视频教程 视频教程​www.bilibili.com 哈喽,大家好,我是小曼(曼哈顿印象),今天给大家带来一期照片转黑白的教程,利用PS快速修出高质量的黑 ...

  7. php 图片二值化在线,图片处理完整流程(包含二值化处理、对黑白照片降噪、边缘去黑像素、三通道转为一通道、图片转array、图片转成任意像素等功能)——可满足一般图片处理要求...

    因为注释给的很详细,所以直接给代码: 1 from PIL import Image 2 # 二值化处理 3 4 5 def 二值化处理(image): 6 for i in range(1, 5): ...

  8. 手写数字图片二值化转换为32*32数组。

    最近课设外加生病,本来打算在上一篇机器学习使用k-近邻算法改进约会网站的配对效果.就打算写的一直没有时间.按照<机器学习实战>的流程,手写数字识别是kNN中的最后一部分,也是一个比较经典的 ...

  9. opencv图片二值化寻找轮廓

    本文主要是介绍如何根据图片的像素值寻找同一像素值的类,并寻找轮廓圈出图片. 1.二值化 函数原型double threshold( InputArray src,OutputArray dst,dou ...

最新文章

  1. [转]svn常用命令
  2. file_operations结构体分析 (设备文件的操作)
  3. 抛出错误_不用try catch,如何机智的捕获错误
  4. Python高级——魔法属性和方法
  5. java 设置主线程_Java线程编程中的主线程讲解
  6. Android持久化保存cookie
  7. 算法笔记_039:杨辉三角形(Java)
  8. Asp.net网站使用HttpHandler实现图片防盗链功能
  9. svn删除文件出错的经验总结
  10. Idea导入jar包的两种方法
  11. 打开Java控制面板
  12. 谈谈奋斗里陆涛为什么不爱米莱
  13. B2B2C 商城系统 WSTMart_v2.0.6_180726程序发布
  14. 无线蓝牙耳机哪个品牌音质好?性价比高音质好的蓝牙耳机排行榜
  15. Unable to load script code in VisualGuidance.The script will not be used until the error 的错误的解决方法
  16. PC上测试移动端网站和模拟手机浏览器
  17. 三星S508手机DIY图文教程全攻略
  18. USBTO232的几个问题,乱码,回车无效,驱动安装
  19. 微信小程序跳转h5链接(web-view)
  20. seaborn palette参数各配色方案及显示效果

热门文章

  1. 对软件架构和企业组织结构的思考
  2. Java编程练习题之冒泡排序
  3. oracle分析函数:一、窗口子句的使用
  4. 【网络工程师备考分享】怎么复习,去哪里找真题练习。
  5. 客流统计大揭秘——各种客流
  6. 0593-CDH5与CDH6对比
  7. floquet端口x极化入射波_厘清平面波、TE(M)波、TE(M)极化、极化几个概念
  8. 阿里巴巴的“达摩院”,必是一场闹剧
  9. JIT是什么,我的简单理解
  10. postsql和geoserver进行连接,并发布服务