栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现10位RAW图转16位RAW图数据并转化成Bitmap

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现10位RAW图转16位RAW图数据并转化成Bitmap

前言:安卓应用中,IR相机的预览回调数据(我测试的是散斑图)格式是RAW10,然后转成RAW16,为了显示回调数据到控件,需要将RAW16格式数据转化成bitmap。

一、10位raw图数据转16位大端raw数据。

    
    public static byte[] transformRAW10ToRAW16(byte[] src, int width, int height) {
        if (src == null || src.length <= 0 || width <= 0 || height <= 0) {
            return null;
        }
        byte[] dst = new byte[width * height * 2];
        int rowStride = src.length / height;
        for (int i = 0; i < height; i++) {
            int k = 0, n = 0, dstStartIndex, srcStartIndex, fillByteStartIndex;
            for (int j = 0; j < width; j += 4) {
                srcStartIndex = i * width + j;
                dstStartIndex = (i * width + k) * 2;
                fillByteStartIndex = i * (rowStride - width) + n;
                if (dstStartIndex >= 0 && dstStartIndex + 7 < dst.length
                        && fillByteStartIndex >= 0 && fillByteStartIndex < src.length) {
                        
                        dst[dstStartIndex] = src[srcStartIndex];
                        dst[dstStartIndex + 1] = (byte) ((src[fillByteStartIndex] << 6) & 0xc0);
                        dst[dstStartIndex + 2] = src[srcStartIndex + 1];
                        dst[dstStartIndex + 3] = (byte) ((src[fillByteStartIndex] << 4) & 0xc0);
                        dst[dstStartIndex + 4] = src[srcStartIndex + 2];
                        dst[dstStartIndex + 5] = (byte) ((src[fillByteStartIndex] << 2) & 0xc0);
                        dst[dstStartIndex + 6] = src[srcStartIndex + 3];
                        dst[dstStartIndex + 7] = (byte) (src[fillByteStartIndex] & 0xc0);
                }
                k+=4;
                n++;
            }

        }

        return dst;
    }

二、raw16大端/小端数据转argb

    // add by zzh 16位无符号灰度图数据,转化成argb数据
    public static int[] getIrARGB(byte[] irBytes, int irWidth, int irHeight) {
        int[] argbData = new int[irWidth * irHeight];
        for (int i = 0; i < irWidth * irHeight; i++) {
            byte[] shortBytes = new byte[]{irBytes[i * 2], irBytes[i * 2 + 1]};
            // 0-65535的灰度数据
            int shortData = getShort(shortBytes, false);
            // 0-255范围的灰度数据
            int grayData = ((shortData * 255) / 65535) & 0x000000ff;
            // R G B A
            argbData[i] = grayData | grayData << 8 | grayData << 16 | 0xff000000;
        }
        return argbData;
    }

    
    public static short getShort(byte[] b, boolean isBigEdian) {
        if (b == null || b.length <= 0 || b.length > 2) {
            return 0;
        }
        if (isBigEdian) {
            return (short) (((b[1] << 8) | b[0] & 0xff));
        } else {
            return (short) (((b[1] & 0xff) | b[0] << 8));
        }
    }

三、argb数据转bitmap

int[] irArgbData = getIrARGB(straightIrRawData, 1280, 720);
Bitmap mIrBitmap = Bitmap.createBitmap(1280, 720, Bitmap.Config.ARGB_8888);
mIrBitmap.setPixels(irArgbData, 0, 1280, 0, 0, 1280, 720);

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940316.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号