Bitmap转灰度字节数组byte[]
工作中遇到图片转灰度数组的须要,经过研究和大神的指导。终于得到例如以下两个方法。能够实现位图转灰度数组
简单的位图转灰度数组就是:得到位图中的每一个像素点,然后依据像素点得到RGB值,最后对RGB值,依据灰度算法得到灰度值就可以
/*如一张480*800的图片,终于得到一个byte[480*800/2]的灰度数组,由于函数把每两个相邻高的像素灰度转化为一个灰度*/
private byte[] java_convertIMG2GreyArray(Bitmap img) {
byte[] theBytes = null;
/* 得到位图的宽高 */
int width = img.getWidth();
int height = img.getHeight();
/* 取得位图的像素点 */
int[] pixels = new int[width * height];
img.getPixels(pixels, 0, width, 0, 0, width, height);
/* 定义结果数据数组 */
theBytes = new byte[width * height / 2];
/*定义循环中用到的变量,节约内存和时间*/
int x, y, k;
int pixel, r, g, b;
for (y = 0; y < height; y++) {
for (x = 0, k = 0; x < width; x++) {
//依次取得像素点
pixel = pixels[y * width + x];
//得到rgb
r = (pixel >> 16) & 0xFF;
g = (pixel >> 8) & 0xFF;
b = pixel & 0xFF;
/*每两行存为一行*/
if (x % 2 == 1) {
theBytes[k + y * width / 2] = (byte) (theBytes[k + y
* width / 2] | ((r * 299 + g * 587 + b * 114 + 500) / 1000) & 0xf0);
k++;
} else {
theBytes[k + y * width / 2] = (byte) (theBytes[k + y
* width / 2] | (((r * 299 + g * 587 + b * 114 + 500) / 1000) >> 4) & 0x0f);
}
}
}
return theBytes;
}
/*灰度依次转换 如:480 * 800最后得到一个byte[480*800]的灰度数组*/
private void java_convertIMGtoGreyArray(Bitmap img) {
boolean usedebug = true;
if (debugImage == null || debugUihandler == null)
usedebug = false;
int width = img.getWidth(); // 获取位图的宽
int height = img.getHeight(); // 获取位图的高
theBytes = null;
theBytes = new byte[width * height];
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
img.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int colorAtPixel = pixels[width * i + j];
int alpha = (colorAtPixel >> 24) & 0xFF;
int red = (colorAtPixel >> 16) & 0xFF;
int green = (colorAtPixel >> 8) & 0xFF;
int blue = colorAtPixel & 0xFF;
theBytes[width * i + j] = (byte) ((red + green + blue) / 3);
int theb = (0xff & theBytes[width * i + j]);
pixels[width * i + j] = alpha << 24 | theb << 16 | theb << 8
| theb;
}
}
bmo = img;
bmo.setPixels(pixels, 0, width, 0, 0, width, height);
pixels = null;
if (debugUihandler != null)
debugUihandler.post(new Runnable() {
@Override
public void run() {
if (debugImage != null && bmo != null)
debugImage.setImageBitmap(bmo);
}
});
}
Bitmap转灰度字节数组byte[]的更多相关文章
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- Java基础知识强化之IO流笔记27:FileInputStream读取数据一次一个字节数组byte[ ]
1. FileInputStream读取数据一次一个字节数组byte[ ] 使用FileInputStream一次读取一个字节数组: int read(byte[] b) 返回值:返回值其实是实际 ...
- C#中字节数组(byte[])和字符串相互转换
转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...
- C#-----字节数组(byte[])和字符串相互转换
Encoding类 表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System ...
- 字节数组byte[]和整型,浮点型数据的转换——Java代码
近期在写C++ socket和java socket之间的通信程序,涉及到整数浮点数的传输.须要从字节数组还原数据,查了一些资料.总结例如以下 1. 整数和浮点数的机器表示 在机器内部.不 ...
- [.Net,C#]三类资源:流对象Stream,字节数组byte[],图片Image
三类资源:流对象Stream,字节数组byte[],图片Image 关系:Stream<=>byte[],byte[]<=>Image Stream 与Image相互转化的媒介 ...
- C#中字节数组byte[]和字符串string类型的相互转换
C#中字节数组byte[]和字符串string类型的相互转换: string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBy ...
- C#--整型与字节数组byte[]之间的转换
using System; int i = 123;byte [] intBuff = BitConverter.GetBytes(i); // 将 int 转换成字节数组lob.Write ...
- java中字节数组byte[]和字符(字符串)之间的转换
转自:http://blog.csdn.net/linlzk/article/details/6566124 Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成by ...
随机推荐
- 求证:a^4+b^4 ≧a^3*b+a*b^3
证明: a4+b4-a3b-ab3 =a3(a-b)-b3(a-b) =(a3-b3)(a-b) =(a-b)2(a2+ab+b2) 而a2+ab+b2=a2+ab+b2/4+3b2/4=(a+b/2 ...
- 符号三角形_hdu_2510(深搜).java
http://acm.hdu.edu.cn/showproblem.php?pid=2510 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- 从业者生存质量报告之,教育行业里的IT男
当教育遇上互联网,非常多传统教育机构都卡在了技术这道门槛上. 一位教育机构创始人曾这样感叹说:"技术须要文化基因.氛围.教育行业不知道技术这帮兄弟须要什么样的文化,什么样的工作氛围,怎么管理 ...
- 算法笔记_146:TarJan算法的应用(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M& ...
- How to make a custom WIDGET in OpenERP
转自:http://sahotaparamjitsingh.blogspot.com/2012/04/how-to-make-custom-widget-in-openerp.html Hello ...
- socket shutdown 与 close 函数 的区别
假设server和client 已经建立了连接,server调用了close, 发送FIN 段给client(其实不一定会发送FIN段,后面再说),此时server不能再通过socket发送和接收数据 ...
- ASP.NET总结
ASP.NET已经学习完.学牛腩的时候面对一些控件和方法会用,但对当中的原理还不懂.学习这部分的内容时, 从头到尾都有一种相识的感觉,把之前一些不懂得地方也理解了,每个知识都有相应的样例练习,学起来还 ...
- spring Log4j关于No appenders could be found for logger的警告
(spring环境下)配置Log4j时候,当启动WEB程序时,提示了如标题的警告,具体如下:log4j:WARN No appenders could be found for logger (org ...
- android 实现全屏代码
设置全屏包括两个部分: 窗口全屏和Activity全屏. 窗口全屏 是指隐藏系统顶部用来显示时间.电量.信号等信息的标题栏 . Activity全屏 是指隐藏程序的标题栏.我们可以通过修改Androi ...
- mosquitto --用户配置 及权限管理
mosquitto中可以添加多个用户,只有使用用户名和密码登陆服务器才允许用户进行订阅与发布操作.可以说用户机制是mosquitto重要的安全机制,增强服务器的安全性.用户与权限配置需要修改3处地方: ...