2

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(); 

  }
Andrew T.
  • 2,088
  • 1
  • 20
  • 42
NewCoder
  • 81
  • 2
  • 11
  • Possible duplicate: http://stackoverflow.com/questions/9325861/converting-yuv-rgbimage-processing-yuv-during-onpreviewframe-in-android – Andrew T. May 13 '15 at 17:25
  • thank you Andrew i've read the thread but this not related. I have also used the method encodeYV12 and getYV12 [link](http://rcos.rpi.edu/projects/miragear/commit/part-1-of-matching-test-finished/) but my application crashes with message array index out of bound exception @Andrew – NewCoder May 13 '15 at 17:45

2 Answers2

0

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);
}
Andrew T.
  • 2,088
  • 1
  • 20
  • 42
  • Hi @Andrew can these values help me in creating a image bitmap same as original image.??? you are right i can have the values by using your method but what would i do with the values only. I want a Image bitmap having YUV format. so that i can move forward with my application. I've searched a lot, tried different ways but as you can see my bad I'm still stuck. – NewCoder May 13 '15 at 19:08
  • Did you try searching for "bitmap to YUV"? The answer to this question is pretty much exactly what you need so long as you know how to convert a byte array to a bitmap. http://stackoverflow.com/a/13055615/902730 – Andrew T. May 13 '15 at 20:47
0

just replace 0's with your image size.

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

It helped me.