android BitmapUtil 工具类
16lz
2021-12-04
package com.common.utils;import android.content.Intent;import android.graphics.*;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.net.Uri;import android.provider.MediaStore;import com.litesuits.common.assist.Base64;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class BitmapUtil { /** * convert Bitmap to byte array */ public static byte[] bitmapToByte(Bitmap b) { ByteArrayOutputStream o = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG, 100, o); return o.toByteArray(); } /** * convert byte array to Bitmap */ public static Bitmap byteToBitmap(byte[] b) { return (b == null || b.length == 0) ? null : BitmapFactory.decodeByteArray(b, 0, b.length); } /** * 把bitmap转换成Base64编码String */ public static String bitmapToString(Bitmap bitmap) { return Base64.encodeToString(bitmapToByte(bitmap), Base64.DEFAULT); } /** * convert Drawable to Bitmap */ public static Bitmap drawableToBitmap(Drawable drawable) { return drawable == null ? null : ((BitmapDrawable) drawable).getBitmap(); } /** * convert Bitmap to Drawable */ public static Drawable bitmapToDrawable(Bitmap bitmap) { return bitmap == null ? null : new BitmapDrawable(bitmap); } /** * scale image */ public static Bitmap scaleImageTo(Bitmap org, int newWidth, int newHeight) { return scaleImage(org, (float) newWidth / org.getWidth(), (float) newHeight / org.getHeight()); } /** * scale image */ public static Bitmap scaleImage(Bitmap org, float scaleWidth, float scaleHeight) { if (org == null) { return null; } Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(org, 0, 0, org.getWidth(), org.getHeight(), matrix, true); } public static Bitmap toRoundCorner(Bitmap bitmap) { int height = bitmap.getHeight(); int width = bitmap.getHeight(); Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, width, height); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(0xff424242); //paint.setColor(Color.TRANSPARENT); canvas.drawCircle(width / 2, height / 2, width / 2, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } public static Bitmap createBitmapThumbnail(Bitmap bitMap, boolean needRecycle) { int width = bitMap.getWidth(); int height = bitMap.getHeight(); // 设置想要的大小 int newWidth = 120; int newHeight = 120; // 计算缩放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片 Bitmap newBitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true); if (needRecycle) bitMap.recycle(); return newBitMap; } public static boolean saveBitmap(Bitmap bitmap, File file) { if (bitmap == null) return false; FileOutputStream fos = null; try { fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); return true; } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } public static boolean saveBitmap(Bitmap bitmap, String absPath) { return saveBitmap(bitmap, new File(absPath)); } /** * 计算图片的缩放值 * 如果图片的原始高度或者宽度大与我们期望的宽度和高度,我们需要计算出缩放比例的数值。否则就不缩放。 * heightRatio是图片原始高度与压缩后高度的倍数, * widthRatio是图片原始宽度与压缩后宽度的倍数。 * inSampleSize就是缩放值 ,取heightRatio与widthRatio中最小的值。 * inSampleSize为1表示宽度和高度不缩放,为2表示压缩后的宽度与高度为原来的1/2(图片为原1/4)。 * * @param options * @param reqWidth * @param reqHeight * @return */ 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; } /** * 根据路径获得图片并压缩返回bitmap用于显示 * * @return */ public static Bitmap getSmallBitmap(String filePath, int w, int h) { final BitmapFactory.Options options = new BitmapFactory.Options(); //该值设为true那么将不返回实际的bitmap不给其分配内存空间而里面只包括一些解码边界信息即图片大小信息 options.inJustDecodeBounds = true;//inJustDecodeBounds设置为true,可以不把图片读到内存中,但依然可以计算出图片的大小 BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, w, h); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false;//重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);// BitmapFactory.decodeFile()按指定大小取得图片缩略图 return bitmap; }
public static Bitmap getBitmapFromResource(Context context, int resId) { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; opt.inPurgeable = true; opt.inInputShareable = true; // 获取资源图片 InputStream is = context.getResources().openRawResource(resId); Bitmap bmp = BitmapFactory.decodeStream(is, null, opt); if (is != null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return bmp; } public static void setImageByResourseId(Context context, ImageView imageView, int resourseId) { if (imageView == null) return; Bitmap bitmap = BitmapUtil.getBitMapFromResource(context, resourseId); if (bitmap == null) return; // InputStream is=context.getResources().openRawResource(drawableId);// Drawable drawable = new BitmapDrawable(context.getResources(), bitmap); imageView.setImageDrawable(drawable); } public static Intent buildGalleryPickIntent(Uri saveTo, int aspectX, int aspectY, int outputX, int outputY, boolean returnData) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("output", saveTo); intent.putExtra("aspectX", aspectX); intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra("return-data", returnData); intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); return intent; } public static Intent buildImagePickIntent(Uri uriFrom, Uri uriTo, int aspectX, int aspectY, int outputX, int outputY, boolean returnData) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uriFrom, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("output", uriTo); intent.putExtra("aspectX", aspectX); intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra("return-data", returnData); intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); return intent; } public static Intent buildCaptureIntent(Uri uri) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); return intent; }}
更多相关文章
- Android在Button按钮上同时显示文字和图片
- Android图片圆角
- ANDROID视图空间代码
- Android(安卓)图片渐变切换效果
- 浅谈android的selector背景选择器
- Android基础类之BaseAdapter
- android 笔记 --- 自定义Android主题风格theme.xml方法
- Android(安卓)lint 删除无用图片文件和配置文件
- android的selector背景选择器