Android RGB到YCbCr的转换并输出到imageView

时间:2018-11-02 07:09:40

标签: android image

我正在做一个图像处理,需要将RGB位图图像转换为YCbCr颜色空间。我检索了每个像素的RGB值,并将转换矩阵应用于该值。

public void convertRGB (View v) {
    if (imageLoaded) {
        int width = inputBM.getWidth();
        int height = inputBM.getHeight();

        int pixel;
        int alpha, red, green, blue;
        int Y,Cb,Cr;

        outputBM = Bitmap.createBitmap(width, height, inputBM.getConfig());

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                pixel = inputBM.getPixel(x, y);
                alpha = Color.alpha(pixel);
                red = Color.red(pixel);
                green = Color.green(pixel);
                blue = Color.blue(pixel);

                Y  =  (int) (0.299     *  red + 0.587  * green + 0.114 * blue);
                Cb =  (int) (128-0.169 *   red-0.331   * green + 0.500 * blue);
                Cr =  (int) (128+0.500 *   red - 0.419 * green - 0.081 * blue);

                int p = (Y << 24) | (Cb << 16) | (Cr<<8);

                outputBM.setPixel(x,y,p);

            }
        }
        comImgView.setImageBitmap(outputBM);
    }
}

问题是他输出的颜色与原稿不同。我尝试使用BufferedImage,但在Android中不起作用

原始:

Original

转换后:

After Convert

我可以知道在Android Java中处理YCbCr图像的正确方法是什么吗?

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码进行设置

ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(your_yuv_data, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] imageBytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
iv.setImageBitmap(image);

查看文档以获取有关YuvImage类的详细说明。

相关问题