Bitmap转灰度字节数组byte[]

摘要:
theBytes=新字节[width*height];int[]像素=newint[width*height];对于(inti=0;i<height;j<width;intalpha=(colorAtPixel>intgreen=)colorAtPixel>

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

简单的位图转灰度数组就是:得到位图中的每一个像素点,然后依据像素点得到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[]》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ABAP术语-Connection Typeshiro配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mysql alter 添加索引

1.添加主键索引 ALTER TABLE `table_name` ADD PRIMARY KEY (`column`) 2.添加唯一索引 ALTER TABLE `table_name` ADD UNIQUE (`column`) 3.添加全文索引 ALTER TABLE `table_name` ADD FULLTEXT (`column`)...

java解析XML学习总结——SAXReader解析xml文件数据

第一种方式: 1. 加入jar包 注意     1.1 xml文件解析时编码要一致(默认UTF-8),出现报错可以在记事本中另存为来更改编码格式。      2.2 jar包两个都需要 2.gao.xml数据如下: <?xml version="1.0" encoding="UTF-8"?> <emps> <emp&...

调用微信接口,模式二

工具类 HttpUtil: package com.rongzhong.utils.weixin;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import...

filter 以及 orderBy的使用

filter用于关键字过滤操作,orderBy用于排序操作,运行界面如下: 点击标题Name与Email实现排序功能,输入框中输入关键字进行过滤,同时实现根据关键字进行过滤后进行排序操作: ng-repeat="user in users | filter:keyword | orderBy:sortField:reverse" index.html &...

IntelliJ IDEA 版本控制(svn、git) 修改文件后,所属目录的颜色也变化

IntelliJ IDEA 的版本控制默认文件修改了,所属目录的颜色是不会变化,这很不方便。如: 修改方法如下: File --> settings --> version control --> 勾选 "show directories with changed descendants" 改了之后效果如下:...

JBoss入门

 很多内容摘自 https://www.jianshu.com/p/4baaf549436b 1.安装目录 安装完Jboss后得目录结构 目录 功能 appclient/ 包含应用程序客户容器的配置细节。 bin/ 包含 Red Hat 企业版 Linux 和微软 Windows 上 JBoss EAP 的启动脚本。 docs/ 许可证文...