android在lcd屏显示图像算法

直接上代码,有不懂的可以看注释,或者评论

  public static final  int kc = 128;//这个值是动态的 根据屏的高得来的  必须是8(一个字节8位)的倍数  比如高在120 <h<=128范围内 这个值都是8  我们需要把8个像素拼成一个字节。
    public static int[] convertToBW(Bitmap bmp) {
        int width = bmp.getWidth(); // 获取位图的宽
        int height = bmp.getHeight(); // 获取位图的高
        //width:250 height:122
        int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
        int add = 0;
        if(height > kc){
            height = kc;//8- height%8;
        }else {
            add = kc - height;
        }
        int[] array = new int[(height + add) * width];
        bmp.getPixels(pixels, 0, width, 0, 0, width, height);
        int alpha = 0xFF << 24;
        int next = 0;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //    Log.e(TAG, "J:" + j + " i:" + i + " width * i:" + (width * i + j));
                int grey = pixels[width * j + i];
                int red = ((grey & 0x00FF0000) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);
                grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
                if(i<120 && j>80){//这里是为了做区域划分,让黑白临界值不同,这样让对比度有区别,比如条形码区域对比度要设置低点,否则会出现线重合在一起,以至于不能读出来。 而文字需要对比度高一点,这个需要更具自己图片对应区域坐标和类型进行改变 。
                    if (grey <= 80) {
                        grey = 0;//0是黑色
                    } else {
                        grey = 1;//1是白色
                    }
                }else {
                    if (grey <= 220) {//这里的80(grey <= 80)和 220是用来做黑白临界值的,这个值越大,黑的就越多,可能就会出现字体显示毛边的情况,而这个值越小会出现字体显示不清晰的情况。
                        grey = 0;
                    } else {
                        grey = 1;
                    }
                }
                next = (height + add) * i + j;
                array[next] = grey;
                // grey = alpha | (grey << 16) | (grey << 8) | grey;
                //pixels[height * i + j] = grey;
            }
            for (int c = 1; c < add + 1; c++) {
                int temp = next + c;
                array[temp] = 1;
            }
        }
        /*Bitmap newBmp = Bitmap.createBitmap(width, height + add, Bitmap.Config.RGB_565);

        newBmp.setPixels(array, 0, width, 0, 0, width, height + add);

        Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 250, height + add);
        saveBmp(resizeBmp);*/

        int[] ints = toByteArray(array);
       /* String str="";
        for(int i=0; i<array.length; i++){
            str += array[i]+",";
        }*/
        // int[] ints = toByteArray(array);
        int length = ints.length;
        String str = "";
        Log.e(TAG, "glength:" + length);
        return ints;
    }

 public static int[] toByteArray(int[] array) {
        int length = array.length;
        int[] byteArray;
        byteArray = new int[4000];/这里4000是根据自己的屏的尺寸来定的
        int reallength = length / 8;
        if(reallength > 4000){
            length = 4000 * 8;
        }else {
            int templen = 4000 - reallength;
            for(int i=1 ;i < templen + 1; i++){
                byteArray[4000 - i] = 255;
            }
        }
        /*if(length%8 == 0){
           byteArray = new int[length/8];
        }else {
           byteArray = new int[length/8 + 1];
        }*/
        Log.e(TAG, "length: " + length);
        for (int i = 0, j = 0; i < length; i += 8, j++) {
            int sum = 0, a;
            for (int k = 0; k < 8; k++) {
                int temp = 1 << k;
                if (i + 7 - k < length) {
                    a = array[i + 7 - k];
                    if (a == 1) {
                        sum = sum | temp;
                    }
                } else {
                    break;
                }
            }
            byteArray[j] = sum;
        }
        return byteArray;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值