Back to project page FrameLite.
The source code is released under:
GNU General Public License
If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.miku.framelite.utils; // ww w. j a va2 s .c o m import java.io.ByteArrayOutputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; import android.util.Base64; import android.view.View; import android.view.View.MeasureSpec; /** * Bitmap??? * * @author xr.lee * */ public class BitmapUtils { /** * ?View?????Bitmap * * @param view * @return */ public static Bitmap convertViewToBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; } /** * ??Bitmap????? * * @param bitmap * @param dstWidth * @param dstHeight * @return */ public static Bitmap scaleBitmap(Bitmap bitmap, int dstWidth, int dstHeight) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) dstWidth / w); float scaleHeight = ((float) dstHeight / h); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); } /** * ?????????bitmap,???dstWidth?dstHeight????? * * @param bitmap * @param dstWidth * @param dstHeight * @return */ public static Bitmap scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth, int dstHeight) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); float ratio = ((float) w) / h; if (dstWidth > dstHeight) { dstWidth = (int) (dstHeight * ratio); } else { dstHeight = (int) (dstWidth / ratio); } return scaleBitmap(bitmap, dstWidth, dstHeight); } /** * ?Drawable?????Bitmap * * @param drawable * @return */ public static Bitmap convertDrawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } /** * ?Bitmap??byte[]?? * * @param bm * @return */ public static byte[] bitmapToBytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } /** * ?bitmap??????base64??? * @param bitmap * @return base64 ??? */ public static String bitmap2Base64(Bitmap bitmap, int bitmapQuality) { // ?Bitmap????????? String string = null; byte[] bytes = bitmapToBytes(bitmap); string = Base64.encodeToString(bytes, Base64.DEFAULT); return string; } /** * ?base64??????bitmap?? * @param string base64??? * @return bitmap */ public static Bitmap base642Bitmap(String string) { // ??????????Bitmap?? Bitmap bitmap = null; try { byte[] bitmapArray; bitmapArray = Base64.decode(string, Base64.DEFAULT); bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length); } catch (Exception e) { e.printStackTrace(); } return bitmap; } }