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

简单的位图转灰度数组就是:得到位图中的每一个像素点,然后依据像素点得到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. 理解 select poll epoll

    举例说明:老师收学生作业,相当于应用层调用I/O操作. 1.老师逐个收学生作业,学生没有做完,只能阻塞等待,收了之后,再去收下一个学生的作业.这显然存在性能问题. 2.怎么解决上面的问题? 老师找个班 ...

  2. [Ext JS 4] 实战之 带week(星期)的日期选择控件

    前言 Ext JS 3 和 Ext JS 4中都有提供日期选择的组件(当然早期版本也有). 但是有一些日期选择的需求是要看到星期,就是日期中的哪一天是这一年的第几周. 遗憾的是Ext js 并没有提供 ...

  3. CommonClassLoader或SharedClassLoader加载的Spring如何访问并不在其加载范围内的用户程序呢

    Question 引自<深入理解Java虚拟机—JVM高级特性与最佳实践>9.2.1,p235 如果有10个WEB应用程序都是用spring来进行组织管理的话,可以把Spring放到Com ...

  4. oracle 三表关联查询

      oracle 三表关联查询 CreationTime--2018年7月4日17点52分 Author:Marydon 左连接实现三表关联 表A--------------------------- ...

  5. OFBiz实战(1):整合Groovy+FreeMaker

    这是OFBiz实战系列的第一篇文件,该系列的目的是整合Groovy+FreeMaker开发一个图书管理系统,阐述在此过程中碰到的一系列问题,以及如何解决这些问题.第一篇文章说明如何使用Groovy+F ...

  6. 转:发一个自己用过的makefile .

    #gcc test.cpp -L. -Wl,-Bdynamic -ltestlib -Wl,-Bstatic -ltestlib  -Wl,-Bdynamic #make clean; make in ...

  7. iOS开发-应用之间的跳转及通信

    Update 2016-08-12: 在Github的Demo上增加Mac自定义Url Scheme,可以在Safari上输入特定协议头打开应用,并传递参数) 简介 我们接下来将要实现应用程序之间的跳 ...

  8. iOS官方文档阅读 基本格式指北

    一些关键词作用 NS_AVAILABLE 表示可用 如 NS_AVAILABLE(NA, 6_0);例如上面这句就是表示 该方法在6.0系统后可用 如果在6.0以下的系统用不了的 或者直接崩溃. NS ...

  9. Redis全方位讲解--主从复制(转载)

    前言 前面介绍了redis持久化和容灾备份,这篇会介绍redis主从复制和redis持久化在主从复制中的一些应用.因为本人没有那么多服务器或机器,所以这里主要介绍下如何在docker容器中搭建主从复制 ...

  10. mosquitto --- 单向认证

    1.生成证书要单向配置SSL 需要 做三项前置工作 1. 生成CA证书 2.生成server 端证书,server 端key github 的一个开源项目已经做到这点 ,详情可见 https://gi ...