开发者

Bitmap resizing and rotating: linear noise

I am resizing image and rotating it using Matrix:

Matrix mtx = new Matrix();
if(orientation>0) {
    mtx.postRotate(orientation);
    Log.d(TAG,"image rotated: "+orientation);
}
if(scale<1) {
    mtx.postScale(scale,scale);
    Log.d(TAG,"image scaled: "+scale);
}
bmp = Bitmap.createBitmap(bm_orig, 0, 0, width, height, mtx, true);
bm_orig.recycle();
bmp.compress(Bitmap.CompressFormat.JPEG,95,output);
bmp.recycle();

When bmp_orig is taken, used 3.2 Mpx Camera, image resized and rotated looks n开发者_StackOverflow中文版ormal.

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?


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
height_tmp/=2;
load_scale*=2;
}
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);

input.close();
//now you can resize and rotate image using matrix as stated in question
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜