TAGS :Viewed: 3 - Published at: a few seconds ago

[ Bitmap resizing and rotating: linear noise ]

I am resizing image and rotating it using Matrix:

Matrix mtx = new Matrix();
if(orientation>0) {
    Log.d(TAG,"image rotated: "+orientation);
if(scale<1) {
    Log.d(TAG,"image scaled: "+scale);
bmp = Bitmap.createBitmap(bm_orig, 0, 0, width, height, mtx, true);

When bmp_orig is taken, used 3.2 Mpx Camera, image resized and rotated looks normal.

But when source is 4 Mpx or bigger, result after resizing has barely-noticeable linear noise

I dont know, why does this noise appear, and how to remove it.

Any idea? May be another way to resize and rotate?

Answer 1

Found that this problem is related with source and resulting image size.

Solved it, when check image size before loading it, and then load halfsized image, if source image size is more than 2 times bigger than resulting size is needed.

BitmapFactory.Options options_to_get_size = new BitmapFactory.Options();
options_to_get_size.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, options_to_get_size);
int load_scale = 1; // load 100% sized image
int width_tmp=options_to_get_size.outWidth
int height_tmp=options_to_get_size.outHeight;

while(width_tmp/2>maxW && height_tmp/2>maxH){
width_tmp/=2;//load half sized image
Log.d(TAG,"load inSampleSize: "+ load_scale);

//Now load image with precalculated scale. scale must be power of 2 (1,2,4,8,16...)
BitmapFactory.Options option_to_load = new BitmapFactory.Options();
option_to_load.inSampleSize = load_scale;
((FileInputStream)input).getChannel().position(0); # reset input stream to read again
Bitmap bm_orig = BitmapFactory.decodeStream(input,null,option_to_load);

//now you can resize and rotate image using matrix as stated in question