位图调整大小和旋转:线性噪声

发布于 2024-10-16 18:49:21 字数 657 浏览 0 评论 0原文

我正在使用矩阵调整图像大小并旋转它:

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或更大时,调整大小后的结果几乎没有明显的线性噪声

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

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

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-10-23 18:49:21

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

解决了这个问题,当在加载之前检查图像大小,然后加载一半大小的图像时,如果源图像大小比结果大小大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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文