【问题标题】:Bitmap resizing and rotating: linear noise位图大小调整和旋转:线性噪声
【发布时间】:2011-02-11 18:41:08
【问题描述】:

我正在调整图像大小并使用 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();

拍摄 bmp_orig 时,使用了 3.2 Mpx 相机,调整大小和旋转后的图像看起来正常。

但当源为 4 Mpx 或更大时,调整大小后的结果几乎没有明显的线性噪声

我不知道,为什么会出现这种噪音,以及如何消除它。

有什么想法? 可能是另一种调整大小和旋转的方式?

【问题讨论】:

    标签: android image matrix resize rotation


    【解决方案1】:

    发现此问题与源和生成的图像大小有关。

    解决了,在加载之前检查图像大小,然后加载一半大小的图像,如果源图像大小是结果大小的 2 倍以上。

    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
    

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      相关资源
      最近更新 更多