package com.itbox.fx.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.MediaStore; public class ImageUtils { public static Bitmap getBitmap(Context context, String path, byte[] data, Uri uri, int reqWidth, int reqHeight) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true; Bitmap bitmap = decode(context, path, data, uri, newOpts); newOpts.inJustDecodeBounds = false; newOpts.inSampleSize = calculateInSampleSize(newOpts, reqWidth, reqHeight); bitmap = decode(context, path, data, uri, newOpts); return bitmap; } // And to convert the image URI to the direct file system path of the image // file public String getRealPathFromURI(Uri contentUri, Context context) { // can post image String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(contentUri, proj, // Which // columns // to // return null, // WHERE clause; which rows to return (all rows) null, // WHERE clause selection arguments (none) null); // Order-by clause (ascending by name) int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } public static Bitmap decode(Context context, String path, byte[] data, Uri uri, BitmapFactory.Options options) { Bitmap result = null; if (path != null) { result = BitmapFactory.decodeFile(path, options); } else if (data != null) { result = BitmapFactory.decodeByteArray(data, 0, data.length, options); } else if (uri != null) { ContentResolver cr = context.getContentResolver(); InputStream inputStream = null; try { inputStream = cr.openInputStream(uri); result = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * * @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) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else { inSampleSize = Math.round((float) width / (float) reqWidth); } } return inSampleSize; } public static void Bitmap2File(Bitmap bitmap, String filename) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); FileOutputStream fos = null; try { fos = new FileOutputStream(filename); fos.write(baos.toByteArray()); fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } } } public static Bitmap getUriBitmap(Context context, Uri uri, int reqWidth, int reqHeight) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true; Bitmap bitmap = decodeUriAsBitmap(context, uri, newOpts); newOpts.inJustDecodeBounds = false; newOpts.inSampleSize = calculateInSampleSize(newOpts, reqWidth, reqHeight); bitmap = decodeUriAsBitmap(context, uri, newOpts); return bitmap; } public static Bitmap decodeUriAsBitmap(Context context, Uri uri, BitmapFactory.Options options) { Bitmap result = null; if (uri != null) { ContentResolver cr = context.getContentResolver(); InputStream inputStream = null; try { inputStream = cr.openInputStream(uri); result = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 将Bitmap转换成InputStream * * @param bm * @return */ public static InputStream bitmap2InputStream(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); InputStream is = new ByteArrayInputStream(baos.toByteArray()); return is; } }