简体   繁体   中英

when I convert my rgb bitmap to yuv it results in a red image. I want to read image from gallery in YUV format or convert the RGB bitmap to YUV

I want to use the Y, U and V channel of the image. How can I target those channels? need help for the conversion. please give me some example code. Here is my code.

public void YUVImage(View view) {
    Bitmap bitmap = ((BitmapDrawable)OrignalImg.getDrawable()).getBitmap();//reading image from imageView

    int bwidth = bitmap.getWidth();
    int bheight = bitmap.getHeight();
    Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas c = new Canvas(mutableBitmap);

    Toast.makeText(this, "Done 1 ok ",
            Toast.LENGTH_LONG).show();  

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);

    Toast.makeText(this, "Done 2 ok ",
          Toast.LENGTH_LONG).show();    

    ColorMatrix yuvMatrix = new ColorMatrix();
    yuvMatrix.setRGB2YUV();

    Toast.makeText(this, "Now Yuv ok ",
          Toast.LENGTH_LONG).show();

    ColorFilter filter = new ColorMatrixColorFilter(yuvMatrix);
    paint.setColorFilter(filter);
    Matrix matrix = new Matrix();
    matrix.setTranslate(2*bwidth, 2*bheight); 
    c.drawBitmap(mutableBitmap, 0, 0, paint);

    Toast.makeText(this, "Canvas ok ",
          Toast.LENGTH_LONG).show();    

    ImageView mImg=null;
    mImg = (ImageView) findViewById(R.id.imageView1);
    mImg.setImageDrawable(new BitmapDrawable(getResources(), mutableBitmap));
    //mImg.setImageBitmap(newBitmap);

     Toast.makeText(this, "YUV Image converted",
             Toast.LENGTH_LONG).show(); 

  }

If you just want the values then simple math is all you need.

void rgb2yuv(int r, int g, int b) {
    int y = (int)(0.299 * r + 0.587 * g + 0.114 * b);
    int u = (int)((b - y) * 0.492f); 
    int v = (int)((r - y) * 0.877f);
}

just replace 0's with your image size.

c.drawBitmap(mutableBitmap, x, y, paint);

It helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM