Bitmap resizing and rotating: linear noise

后端 未结 1 1251
既然无缘
既然无缘 2021-02-11 08:16

I am resizing image and rotating it using Matrix:

Matrix mtx = new Matrix();
if(orientation>0) {
    mtx.postRotate(orientation);
    Log.d(TAG,\"image rotate         


        
1条回答
  •  梦毁少年i
    2021-02-11 08:47

    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 讨论(0)
提交回复
热议问题