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

简单的位图转灰度数组就是:得到位图中的每一个像素点,然后依据像素点得到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. 使用nmonchart把.nmon文件转换成html

    转载:https://blog.csdn.net/zd470015321/article/details/68923280 我的环境 :centos6.6 下载地址 nmon: http://nmon ...

  2. data目录和binlog目录搬迁的方法

    刚开始安装时使用了默认目录,使用一段时间,数据慢慢变在,发现当前设置的目录空间不够时,就要搬迁数据到另一个目录了 如果全过程使用的是Mysql用户,应该可以正常启动. 如果用的ROOT用户,可能不能正 ...

  3. HTTP浏览器缓存机制

    来自:http://kb.cnblogs.com/page/165307/ 浏览器缓存机制 浏览器缓存机制,其实主要就是HTTP协议定义的缓存机制(如: Expires: Cache-control等 ...

  4. proxy [ˈprɒksi] 代理

    谷歌.QQ支持,360.搜狗不支持 使用proxy代理可以实现,对原先的类不进行处理,新建一个类,对此进行拦截处理,以实现低耦合 可以通过代理拦截的方法,将方法进行重写. //------------ ...

  5. oracle 对系统日期sysdate进行格式化:to_date(sysdate,'yyyy-MM-dd')

      oracle 对系统日期sysdate进行格式化:to_date(sysdate,'yyyy-MM-dd') CreateTime--2018年5月17日10:11:34 Author:Maryd ...

  6. 14-spring学习-变量操作

    表达式所有操作都是可以以变量形式出现的. 观察变量的定义: package com.Spring.ELDemo; import org.springframework.expression.Evalu ...

  7. React 属性和状态具体解释

    属性的含义和使用方法 props=properties 属性:一个事物的性质与关系 属性往往是与生俱来的.无法自己改变的. 属性的使用方法: 第一种方法:键值对 1.传入一个字符串:"Hi& ...

  8. 【LeetCode】69. Sqrt(x) (2 solutions)

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...

  9. Mosquitto----服务器日志

    客户端连接日志 1403334375: New connection from 121.201.8.163 on port 1883. 1403334375: New client connected ...

  10. tp类型自动转换和自动完成

    一.类型自动转换 在模型中对数据库字段自动完成 //模型中数据类型自动转换 添加一个$type数组即可protected $type=array( 'username'=>'serialize' ...