2023-02-24 android app 获取raw 图像数据,把数据装入Bitmap里面,然后在ImageView显示出来,主要用到Bitmap.createBitmap、setPixels

一、核心java代码,主要用到Bitmap.createBitmap、setPixels等函数

public Bitmap bitmap;

bitmap = Bitmap.createBitmap(w[0], h[0], Bitmap.Config.ARGB_8888);
int[] irArgbData = getIrARGB(image_buf, w[0], h[0]);
bitmap.setPixels(irArgbData, 0, w[0], 0, 0, w[0], h[0]);

public ImageView rgbImageView;
rgbImageView.setImageBitmap(bitmap);



    // add by 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, true);
            // 0-255范围的灰度数据
           // int grayData = ((shortData * 255) / 65535) & 0x000000ff;
            int grayData = (shortData >> 2) & 0x000000ff;

            // R G B A
            argbData[i] = grayData | grayData << 8 | grayData << 16 | 0xff000000;
        }
        return argbData;
    }

    /**大端、小端方式,两字节数据转short值(有符号)**/
    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));
        }
    }

二、效果图,摄像头上面是一包漫花纸巾

三、参考文章

Java实现10位RAW图转16位RAW图数据并转化成Bitmap_raw10和raw16_Zafir2023的博客-CSDN博客前言:安卓应用中,IR相机的预览回调数据(我测试的是散斑图)格式是RAW10,然后转成RAW16,为了显示回调数据到控件,需要将RAW16格式数据转化成bitmap。一、10位raw图数据转16位大端raw数据。 /** * 非安卓标准格式的RAW10转RAW16,补充数据在末尾 * @param src * @param width * @param height * @return 大端格式的raw16数据 */ pubhttps://blog.csdn.net/zzhceo/article/details/125070668

猜你喜欢

转载自blog.csdn.net/qq_37858386/article/details/129207562
今日推荐