記錄一個好用的Bitmap工具類
阿新 • • 發佈:2019-01-11
package com.why.project.bitmaputildemo.utils; 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 java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class BitmapUtil { public static Bitmap temp; /**根據指定的高度進行縮放(source是bitmap)*/ public static Bitmap bitmapZoomByHeight(Bitmap srcBitmap, float newHeight) { float scale = newHeight / (((float)srcBitmap.getHeight())); return BitmapUtil.bitmapZoomByScale(srcBitmap, scale, scale); } /**根據指定的高度進行縮放(source是drawable)*/ public static Bitmap bitmapZoomByHeight(Drawable drawable, float newHeight) { Bitmap bitmap = BitmapUtil.drawableToBitmap(drawable); float scale = newHeight / (((float)bitmap.getHeight())); return BitmapUtil.bitmapZoomByScale(bitmap, scale, scale); } /**根據指定的寬度比例值和高度比例值進行縮放*/ public static Bitmap bitmapZoomByScale(Bitmap srcBitmap, float scaleWidth, float scaleHeight) { int width = srcBitmap.getWidth(); int height = srcBitmap.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap bitmap = Bitmap.createBitmap(srcBitmap, 0, 0, width, height, matrix, true); if(bitmap != null) { return bitmap; }else { return srcBitmap; } } /**將drawable物件轉成bitmap物件*/ public static Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; Bitmap bitmap = Bitmap.createBitmap(width, height, config); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return bitmap; } /**將drawable物件轉成bitmap物件*/ public static Bitmap drawableToBitmap2(Drawable drawable) { BitmapDrawable bd = (BitmapDrawable) drawable; Bitmap bm= bd.getBitmap(); return bm; } /**將base64圖片物件轉成bitmap物件*/ public static Bitmap base64ToBitmap(String string, int optionSize) { Bitmap bitmap = null; try { byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT); bitmap = byteToBitmap(bitmapArray, optionSize); } catch (Exception e) { e.printStackTrace(); } return bitmap; } public static Bitmap byteToBitmap(byte[] imgByte, int optionSize) { InputStream input = null; Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = optionSize; options.inScaled = true; options.inPreferredConfig = Bitmap.Config.ARGB_8888; input = new ByteArrayInputStream(imgByte); SoftReference softReference = new SoftReference(BitmapFactory.decodeStream(input, null, options)); bitmap = (Bitmap) softReference.get(); if (imgByte != null) { imgByte = null; } try { if (input != null) { input.close(); } } catch (IOException e) { e.printStackTrace(); } return bitmap; } /**將bitmap物件儲存成圖片到sd卡中*/ public static void saveBitmapToSDCard(Bitmap bitmap, String path) { File file = new File(path); if(file.exists()) { file.delete(); } try { FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, ((OutputStream)fileOutputStream));//設定PNG的話,透明區域不會變成黑色 fileOutputStream.close(); System.out.println("----------save success-------------------"); } catch(Exception v0) { v0.printStackTrace(); } } /**從sd卡中獲取圖片的bitmap物件*/ public static Bitmap getBitmapFromSDCard(String path) { Bitmap bitmap = null; try { FileInputStream fileInputStream = new FileInputStream(path); if(fileInputStream != null) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; //當圖片資源太大的適合,會出現記憶體溢位。圖片寬高都為原來的二分之一,即圖片為原來的四分一 bitmap = BitmapFactory.decodeStream(((InputStream) fileInputStream), null, options); } } catch(Exception e) { return null; } return bitmap; } }