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 ...
随机推荐
- (C++)虚函数表解析(转)
(文章出处不详,转自:http://blog.csdn.net/hairetz/article/details/4137000) C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用 ...
- (C++)浅谈多态基类析构函数声明为虚函数
主要内容: 1.C++类继承中的构造函数和析构函数 2.C++多态性中的静态绑定和动态绑定 3.C++多态性中析构函数声明为虚函数 1.C++类继承中的构造函数和析构函数 在C++的类继承中, 建立对 ...
- (剑指Offer)面试题4:替换空格
题目: 请实现一个函数,把字符串中的每个空格替换成“%20”,例如输入“We are happy”,则输出“We%20are%20happy”. 思路: 背景: 在网络编程中,如果URL参数中含有特殊 ...
- gedit如何使用代码片段
gedit的片段插件 1 打开这个插件 2 以HTML为例,我们看到触发器是doctype 3则我在编辑一个HTML文档的时候,先输入doctype,再按Tab键,将会自动提示,我选择即可插入 ...
- es5 - array - shift
/** * 描述:该shift()方法从数组中删除第一个元素并返回已删除的元素.此方法更改数组的长度. * 语法:arr.shift() * 返回:该shift方法删除零点索引处的元素并将连续索引处的 ...
- uva10401Injured Queen Problem(递推)
题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...
- 算法笔记_045:币值最大化问题(Java)
目录 1 问题描述 2 解决方案 2.1 动态规划法 1 问题描述 给定一排n个硬币,其面值均为正整数c1,c2,...,cn,这些整数并不一定两两不同.请问如何选择硬币,使得在其原始位置互不相邻 ...
- 如何打印加密的PDF文件?
如何打印加密的PDF文件? Pdf加密了不让打印怎么办?? 下载Foxit PDF Editor以下是下载地址:http://www.orsoon.com/Soft/4865.html 用它打开加密的 ...
- js正则表达式test方法、exec方法与字符串search方法区别
1.正则表达式test方法 test() 方法用于检测一个字符串是否匹配某个模式 返回值: 如果字符串 string 中含有与 RegExpObject 匹配的文本,则返回 true,否则返回 fal ...
- Android 常用的性能分析工具详解:GPU呈现模式, TraceView, Systrace, HirearchyViewer(转)
此篇将重点介绍几种常用的Android性能分析工具: 一.Logcat 日志 选取Tag=ActivityManager,可以粗略地知道界面Displaying的时间消耗.当我们打开一个Activit ...