工作中遇到图片转灰度数组的须要,经过研究和大神的指导。终于得到例如以下两个方法。能够实现位图转灰度数组

简单的位图转灰度数组就是:得到位图中的每一个像素点,然后依据像素点得到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[]的更多相关文章

  1. c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换

    字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...

  2. Java基础知识强化之IO流笔记27:FileInputStream读取数据一次一个字节数组byte[ ]

    1. FileInputStream读取数据一次一个字节数组byte[ ]  使用FileInputStream一次读取一个字节数组: int read(byte[]  b) 返回值:返回值其实是实际 ...

  3. C#中字节数组(byte[])和字符串相互转换

    转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...

  4. C#-----字节数组(byte[])和字符串相互转换

       Encoding类  表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System ...

  5. 字节数组byte[]和整型,浮点型数据的转换——Java代码

    近期在写C++ socket和java socket之间的通信程序,涉及到整数浮点数的传输.须要从字节数组还原数据,查了一些资料.总结例如以下 1.       整数和浮点数的机器表示 在机器内部.不 ...

  6. [.Net,C#]三类资源:流对象Stream,字节数组byte[],图片Image

    三类资源:流对象Stream,字节数组byte[],图片Image 关系:Stream<=>byte[],byte[]<=>Image Stream 与Image相互转化的媒介 ...

  7. C#中字节数组byte[]和字符串string类型的相互转换

    C#中字节数组byte[]和字符串string类型的相互转换: string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBy ...

  8. C#--整型与字节数组byte[]之间的转换

    using System; int  i = 123;byte [] intBuff = BitConverter.GetBytes(i);     // 将 int 转换成字节数组lob.Write ...

  9. java中字节数组byte[]和字符(字符串)之间的转换

    转自:http://blog.csdn.net/linlzk/article/details/6566124 Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成by ...

随机推荐

  1. C#中Split用法~字符串分隔

    1.用字符串分隔: using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray= ...

  2. (剑指Offer)面试题48:不能被继承的类

    题目: 写一个不能被继承的类 思路: 1.把构造函数设为私有函数 在C++中子类的构造函数会自动调用父类的构造函数,子类的析构函数也会自动调用父类的构造函数,要想一个类不能被继承,只要把它的构造函数和 ...

  3. Binwalk:后门(固件)分析利器

    http://blog.csdn.net/testing_is_believing/article/details/14091179 Binwalk介绍 Binwalk是一个固件的分析工具,旨在协助研 ...

  4. es6 - 回调深渊

    带着以下疑问来解答以下问题,你会学到很多 1. 回调深渊为什么会出现,为什么需要回调 ? 2. ES6为什么会出现Promise? 3. 回调的作用是什么? 跟闭包有关吗? ES5 ~ ES6 回调深 ...

  5. UML回想-通信图

        我们对软件project这一大块的学习事实上開始的还是挺早的,而且在后来的学习过程中也不断的涉及到了这些知识. 可是,经过软考的检验来看我对软工这一块的内容掌握的实在是慘不忍睹.基本上就是一出 ...

  6. 关于ibatis的实体类部分属性无法映射

    今天在编码中不小心就遇到了一个ibatis初学者偶尔会遇到的问题. 先来看这张图中的DictionPo的部分属性赋值都为空.其实,数据库中是有数据的. 再来看看mapper的写法 上面那个对象查询语句 ...

  7. 【Linux】(2013-7-19)配置tftp与开发板传送文件

    1. 安装必须软件 sudo apt-get install -y xinetd tftp-hpa 2. 修改配置文件 vi /etc/default/tftpd-hpa # /etc/default ...

  8. 【Django】pip 安装和卸载 Django

    1.在dos命令中输入pip进行安装 注意:如果提示('pip' 不是内部或外部命令,也不是可运行的程序或批处理文件.) 那么先将pip添加到环境变量中,pip路径一般在python的安装路径下,例如 ...

  9. PL/SQL详细介绍,设置oracle相关

    1. 实现参照完整性      指若两个表之间具有主从关系(即主外键关系),当删除主表数据时,必须确保相关的从表数据已经被删除.  当修改主表的主键列数据时,必须确保相关从表数据已经被修改.为了实现级 ...

  10. Eclipse使用教程之精华篇

    插件安装方法 插件大概有三种安装方法: 第一种:知道在线安装地址.Eclipse→Help→Install New Software...→地址栏(Work with)中输入安装地址→勾选要安装的插件 ...