Android图片的处理工具类BitmapUtils
项目中经常会用到图片,所以在这先简单的总结一下。闲言少叙,上代码。
package com.lvstudio.myapp.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by LvStudio on 2016/11/7.
*/
public class BitmapUtils {
/**
* 屏幕分辨率和指定清晰度的图片压缩方法
*
* @param context
* @param image Bitmap图片
* @return
*/
public static Bitmap comp(Context context, Bitmap image) {
int maxLength = 1024 * 1024;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length > maxLength) {
options -= 10;
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bais, null, opts);
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int scaleX = opts.outWidth / manager.getDefaultDisplay().getWidth();
int scaleY = opts.outHeight / manager.getDefaultDisplay().getHeight();
int scale = scaleX > scaleY ? scaleX : scaleY;
opts.inJustDecodeBounds = false;
opts.inSampleSize = scale > 1 ? scale : 1;
return BitmapFactory.decodeStream(bais, null, opts);
}
/**
* 屏幕分辨率和指定清晰度的图片压缩方法
*
* @param context
* @param path 图片的路径
* @return
*/
public static Bitmap comp(Context context, String path) {
return compressImage(getUsableImage(context, path));
}
/**
* 获取屏幕分辨率的Bitmap
*
* @param context
* @param path 图片的路径
* @return
*/
public static Bitmap getUsableImage(Context context, String path) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
DisplayMetrics metrics = new DisplayMetrics();
metrics = context.getApplicationContext().getResources().getDisplayMetrics();
int scaleX = opts.outWidth / metrics.widthPixels;
int scaleY = opts.outHeight / metrics.heightPixels;
int scale = scaleX > scaleY ? scaleX : scaleY;
opts.inJustDecodeBounds = false;
opts.inSampleSize = scale > 1 ? scale : 1;
return BitmapFactory.decodeFile(path, opts);
}
/**
* 压缩图片清晰度,到指定大小
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
int maxLength = 1024 * 1024;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length > maxLength) {
options -= 10;
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
/**
* 指定分辨率和清晰度的图片压缩方法
*
* @param fromFile
* @param toFile
* @param reqWidth
* @param reqHeight
* @param quality
*/
public static void transImage(String fromFile, String toFile, int reqWidth, int reqHeight, int quality) {
Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
float scaleWidth = (float) reqWidth / bitmapWidth;
float scaleHeight = (float) reqHeight / bitmapHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
bitmap2File(toFile, quality, resizeBitmap);
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
if (!resizeBitmap.isRecycled()) {
resizeBitmap.recycle();
}
}
/**
* Bitmap转换为文件
*
* @param toFile
* @param quality
* @param bitmap
* @return
*/
public static File bitmap2File(String toFile, int quality, Bitmap bitmap) {
File captureFile = new File(toFile);
FileOutputStream out = null;
try {
out = new FileOutputStream(captureFile);
if (bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)) {
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return captureFile;
}
/**
* Drawable转换为Bitmap
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitamp(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
/**********************************************************/
public static InputStream bitmap2Input(Bitmap bitmap, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
return new ByteArrayInputStream(baos.toByteArray());
}
public static InputStream bitmap2Input(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return new ByteArrayInputStream(baos.toByteArray());
}
public static byte[] bitmap2ByteArray(Bitmap bitmap, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
return baos.toByteArray();
}
public static byte[] bitmap2ByteArray(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
public static byte[] drawable2ByteArray(Drawable drawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
return out.toByteArray();
}
public static Bitmap byteArray2Bitmap(byte[] bytes) {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}