3

rawData我从库函数中获取类型为byte[]RGBA、大小为 640x480、每像素 4 个字节的原始相机数据。我需要将其转换为位图并显示在屏幕上的 ImageView 中。

我要做的是:

byte[] JPEGData = convertToJpeg(rawData, 640, 480, 80);
Bitmap bitmap = BitmapFactory.decodeByteArray(JPEGData , 0, JPEGData .length);    
imageView.setImageBitmap(bitmap);

其中convertToJpeg()函数是:

    public static byte[] convertToJpeg(byte[] buffer, int w, int h, int quality) {
        YuvImage yuv_image = new YuvImage(buffer, ImageFormat.NV21, w, h, null);

        Rect rect = new Rect(0, 0, w, h);
        ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
        yuv_image.compressToJpeg(rect, quality, output_stream);

        Bitmap sourceBmp = BitmapFactory.decodeByteArray(output_stream.toByteArray(), 0, output_stream.size());
        Bitmap destBmp = Bitmap.createScaledBitmap(sourceBmp, (int) (w * 0.75), (int) (h * 0.75), true);

        ByteArrayOutputStream pictureStream = new ByteArrayOutputStream();
        destBmp.compress(CompressFormat.JPEG, quality, pictureStream);
        byte[] pictureByteArray = pictureStream.toByteArray();

        return pictureByteArray;
    }

打电话后decodeByteArray()我有bitmap.getConfig() == ARGB_8888

然而,我在屏幕上看到的是一些混乱的画面,原始画面中有一些模糊的绿色形状。

它出什么问题了?

4

2 回答 2

7

事实证明,解决方案很简单。并且无需转换为 JPEG。

Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bm.copyPixelsFromBuffer(ByteBuffer.wrap(rawData));
于 2014-08-28T15:10:12.943 回答
0

ByteBuffer().rewind 将缓冲区位置重置为 0,而不是解决“缓冲区不够大以容纳像素”的问题

于 2019-12-17T06:24:23.357 回答