Bitmapオブジェクト、寸法圧縮、質量圧縮について
6456 ワード
一:いつ画像の品質を圧縮する必要がありますか.
一般的に、私たちはネット上で画像をアップロードするときに画像の品質(体積)を圧縮しなければなりません.サーバーが画像の大きさに制限があるからです.
二:いつ画像のサイズを圧縮する必要がありますか.
サーバから要求される画像は通常Bitmapオブジェクトに変換されますが、画像のサイズが大きいほど、通常必要なBitmapも大きくなり、oomを防ぐために、通常は画像をサイズ圧縮します.
三:BitmapではBitmapFactoryを使います.Optionsというクラスなので、このクラスのいくつかの属性について説明しておきます
options.inJustDecodeBounds = true;//trueに設定すると、BitmapFactoryでオブジェクトを返すとき、本当にbitmapを返すのではなく、このbitmapの幅と高さに戻り、oomを防ぐ
options.inPurgeable = true;//trueに設定システムメモリが不足している場合はbitmapオブジェクトを回収できますが、再コンパイルが必要な場合はbitmapを格納した元のデータにアクセスできます.
opts.inInputShareable = true;//深くコピーするかどうかを設定し、inPurgeableと組み合わせて使用します.
options.inPreferredConfig = Config.RGB_565;//デフォルトARGB_8888をRGB_に変更565、メモリを半分節約します.
Bitmap.Config ARGB_4444:各画素が4ビット、すなわちA=4、R=4、G=4、B=4を占めると、1画素点が4+4+4+4=16ビットBitmapを占める.Config ARGB_8888:各画素が4ビット、すなわちA=8、R=8、G=8、B=8を占めると、1画素点が8+8+8+8+8=32ビットBitmapを占める.Config RGB_565:各画素が4ビット、すなわちR=5、G=6、B=5を占める透明度がない場合、1画素点が5+6+5=16ビットBitmapを占める.Config ALPHA_8:ピクセルあたり4ビット、透明度のみ、色なし
四:画像サイズ圧縮の書き方
画像圧縮の原理は、画素点を抽出し、解像度(画素密度の大きさは変わらない)で、画像のサイズだけを変えることです.
一般的に、私たちはネット上で画像をアップロードするときに画像の品質(体積)を圧縮しなければなりません.サーバーが画像の大きさに制限があるからです.
二:いつ画像のサイズを圧縮する必要がありますか.
サーバから要求される画像は通常Bitmapオブジェクトに変換されますが、画像のサイズが大きいほど、通常必要なBitmapも大きくなり、oomを防ぐために、通常は画像をサイズ圧縮します.
三:BitmapではBitmapFactoryを使います.Optionsというクラスなので、このクラスのいくつかの属性について説明しておきます
options.inJustDecodeBounds = true;//trueに設定すると、BitmapFactoryでオブジェクトを返すとき、本当にbitmapを返すのではなく、このbitmapの幅と高さに戻り、oomを防ぐ
options.inPurgeable = true;//trueに設定システムメモリが不足している場合はbitmapオブジェクトを回収できますが、再コンパイルが必要な場合はbitmapを格納した元のデータにアクセスできます.
opts.inInputShareable = true;//深くコピーするかどうかを設定し、inPurgeableと組み合わせて使用します.
options.inPreferredConfig = Config.RGB_565;//デフォルトARGB_8888をRGB_に変更565、メモリを半分節約します.
Bitmap.Config ARGB_4444:各画素が4ビット、すなわちA=4、R=4、G=4、B=4を占めると、1画素点が4+4+4+4=16ビットBitmapを占める.Config ARGB_8888:各画素が4ビット、すなわちA=8、R=8、G=8、B=8を占めると、1画素点が8+8+8+8+8=32ビットBitmapを占める.Config RGB_565:各画素が4ビット、すなわちR=5、G=6、B=5を占める透明度がない場合、1画素点が5+6+5=16ビットBitmapを占める.Config ALPHA_8:ピクセルあたり4ビット、透明度のみ、色なし
四:画像サイズ圧縮の書き方
画像圧縮の原理は、画素点を抽出し、解像度(画素密度の大きさは変わらない)で、画像のサイズだけを変えることです.
/**
*
* @param imagePath
* @param maxWidth
* @param maxHeight
* @return
*/
@SuppressWarnings("finally")
public static Bitmap getImageThumbnail(String imagePath, int maxWidth,
int maxHeight, boolean isDeleteFile) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
options.inInputShareable = true;
options.inPreferredConfig = Config.RGB_565;
bitmap = BitmapFactory.decodeFile(imagePath, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
FileInputStream inputStream = null;
File file = new File(imagePath);
try {
if(file != null && file.exists()){
inputStream = new FileInputStream(file);
bitmap = BitmapFactory.decodeStream(inputStream, null,options);
if(isDeleteFile){
file.delete();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
:
, (file), , bitmap
/**
* , , ,
* @param image
* @param size , kb
* @param options , 1-100, , 30, 70%
* @return
*/
private static Bitmap compressImage(Bitmap image,int size,int options) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// , 100 , baos
image.compress(Bitmap.CompressFormat.JPEG, 80, baos);
// 100kb,
while (baos.toByteArray().length / 1024 > size) {
options -= 10;// 10
baos.reset();// baos baos
// options%, baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
// baos ByteArrayInputStream
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
// ByteArrayInputStream
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
:
/**
* ,
* @param imgUrl
* @param reqWidth
* @param size , kb
* @param quality , 1-100, , 30, 70%
* @return Bitmap
*/
public static Bitmap compressBitmap(String imgUrl,int reqWidth,int size,int quality){
// bitMap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgUrl, options);
int height = options.outHeight;
int width = options.outWidth;
int reqHeight;
reqHeight = (reqWidth * height) / width;
// bitmap ,
options.inSampleSize = calculateInSampleSize(
options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(
imgUrl, options);
Bitmap mBitmap = compressImage(Bitmap.createScaledBitmap(
bm, 480, reqHeight, false),size,quality);
return mBitmap;
}
/**
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}