Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created June 17, 2011 03:25
Show Gist options
  • Select an option

  • Save codeswimmer/1030811 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/1030811 to your computer and use it in GitHub Desktop.
Android: possibilities for converting an image from RGB to YUV (which comes in handy for QR code decoding)
Canvas c = new Canvas(bitmap);
ColorMatrix yuvMatrix = new ColorMatrix();
yuvMatrix.setRGB2YUV();
ColorFilter filter = new ColorMatrixColorFilter(yuvMatrix);
// TODO: the rest...
/*************************************************************************
static void setBackground(View v, Bitmap bm) {
if (bm == null) {
v.setBackgroundResource(0);
return;
}
int vwidth = v.getWidth();
int vheight = v.getHeight();
int bwidth = bm.getWidth();
int bheight = bm.getHeight();
float scalex = (float) vwidth / bwidth;
float scaley = (float) vheight / bheight;
float scale = Math.max(scalex, scaley) * 1.3f;
Bitmap.Config config = Bitmap.Config.ARGB_8888;
Bitmap bg = Bitmap.createBitmap(vwidth, vheight, config);
Canvas c = new Canvas(bg);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
ColorMatrix greymatrix = new ColorMatrix();
greymatrix.setSaturation(0);
ColorMatrix darkmatrix = new ColorMatrix();
darkmatrix.setScale(.3f, .3f, .3f, 1.0f);
greymatrix.postConcat(darkmatrix);
ColorFilter filter = new ColorMatrixColorFilter(greymatrix);
paint.setColorFilter(filter);
Matrix matrix = new Matrix();
matrix.setTranslate(-bwidth/2, -bheight/2); // move bitmap center to origin
matrix.postRotate(10);
matrix.postScale(scale, scale);
matrix.postTranslate(vwidth/2, vheight/2); // Move bitmap center to view center
c.drawBitmap(bm, matrix, paint);
v.setBackgroundDrawable(new BitmapDrawable(bg));
}
*************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment