Bitmap.com preSS在过大文件的结果 [英] Bitmap.compress results in too large of file

查看:103
本文介绍了Bitmap.com preSS在过大文件的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要调整图像,然后将其保存为JPG格式的应用程序。我的测试图像是在天空中非常平滑渐变的照片。我试图将它保存使用此code调整大小后JPEG:

I have an app that needs to resize an image and then save it to jpg. My test image is a photo with very smooth gradients in the sky. I'm trying to save it to jpeg after a resize using this code:

dstBmp = Bitmap.createBitmap(srcBmp, cropX, 0, tWidth, srcBmp.getHeight());
if (android.os.Build.VERSION.SDK_INT > 12) {
    Log.w("General", "Calling setHasAlpha");
    dstBmp.setHasAlpha(true);
}
dstBmp = Bitmap.createScaledBitmap(dstBmp, scaledSmaller, scaledLarger, true);
OutputStream out = null;
File f = new File(directory + "/f"+x+".jpg");
try {
    f.createNewFile();
    out = new FileOutputStream(f);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

dstBmp.compress(CompressFormat.JPEG, jpegQuality, out);

的问题是,除非我凸点质量高达约95.然而,在质量水平95,所得到的文件超过150KB过度绑扎发生在图像的梯度。当我执行在Photoshop这些相同的功能,做一个另存为网页,我能避免绑扎一路下跌到质量水平40 50KB的图像尺寸。我的Web服务器上使用ImageCR我可以在30KB完成相同的。

The problem is that excessive banding occurs in the gradients of the image unless I bump the quality up to around 95. However, at quality level 95, the resulting file is over 150kb. When I perform these same functions in photoshop and do a "Save for web", I can avoid banding all the way down to quality level 40 with an image size of 50kb. Using ImageCR on my web server I can accomplish the same at 30kb.

有没有办法在Java中的COM preSS图像成JPEG更有效,或者是有一个单独的库或API,我可以用这样做呢?我加载大量的图片到内存中,并以这样的速度应用在旧设备上的威胁OOM错误。我很乐意有更多的时间分配给图像处理,如果这将有助于最终结果。

Is there any way in Java to compress an image into jpeg more efficiently, or is there a separate library or api I can use to do so? I'm loading a lot of images into memory and at this rate the app is threatening OOM errors on older devices. I'd be happy to allocate more time to image manipulation if that will help the final result.

推荐答案

尝试从的 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

总之,你有2个静态方法

In short, you have 2 static methods

第一计算图像的新尺寸

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

第二个为按比例大小的图片加载到内存中:

second for loading the scaled size image into memory:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

和您可以这样调用这个函数:

and You can call this function like this:

Bitmap b = decodeSampledBitmapFromResource(getResources(), R.id.myimage, 128, 128));

您可以通过修改采取输入parametar字符串(路径如果图像是SD卡上的文件),而不是资源的第二种方法,而不是de codeResource使用:

You can modify the second method to take input parametar String (the path if the image is file on sdcard) instead of resource, and instead of decodeResource use:

BitmapFactory.decodeFile(path, options);

我总是加载大图时使用此code。

I always use this code when loading big images.

这篇关于Bitmap.com preSS在过大文件的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆