Created
August 11, 2015 12:34
-
-
Save deskid/fa49e112cd2fb43f300b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImageUtils { | |
public static final String TMP_CAPTURE_FILE_NAME = "tmp.jpg"; | |
private static final String CACHE_FILE_DIR = "/app"; | |
public static Bitmap getCircle(Bitmap bitmap, boolean recyle) { | |
if (null == bitmap) | |
return null; | |
int w = bitmap.getWidth(); | |
int h = bitmap.getHeight(); | |
if (w <= 0 || h <= 0) { | |
return null; | |
} | |
int r = (w < h ? w : h) / 2; | |
Bitmap circle = Bitmap.createBitmap(2 * r, 2 * r, | |
Bitmap.Config.ARGB_4444); | |
Canvas canvas = new Canvas(circle); | |
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); | |
p.setColor(Color.RED); | |
canvas.drawCircle(r, r, r, p); | |
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); | |
// 生成结果图片 | |
Bitmap out = Bitmap.createBitmap(2 * r, 2 * r, Bitmap.Config.ARGB_4444); | |
Canvas outCan = new Canvas(out); | |
outCan.drawBitmap(bitmap, 0, 0, null); | |
outCan.drawBitmap(circle, 0, 0, p); | |
circle.recycle(); | |
// bitmap.recycle(); | |
if (recyle) { | |
// bitmap.recycle(); | |
} | |
return out; | |
} | |
private static int readPicOrientation(String path) { | |
int orientation = ExifInterface.ORIENTATION_NORMAL; | |
if (TextUtils.isEmpty(path)) | |
return 0; | |
try { | |
ExifInterface exifInterface = new ExifInterface(path); | |
orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return orientation; | |
} | |
private static Bitmap rotateFlipBitmap(Bitmap bitmapSrc, int exifOrientation) { | |
Bitmap bitmapDst = null; | |
Matrix transformMatrix = new Matrix(); | |
switch (exifOrientation) { | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
transformMatrix.setRotate(-90.0f); | |
break; | |
case ExifInterface.ORIENTATION_TRANSPOSE: | |
transformMatrix.setRotate(90.0f); | |
transformMatrix.postScale(-1.0f, 1.0f); | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
transformMatrix.setRotate(90.0f); | |
break; | |
case ExifInterface.ORIENTATION_TRANSVERSE: | |
transformMatrix.setRotate(-90.0f); | |
transformMatrix.postScale(-1.0f, 1.0f); | |
break; | |
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: | |
transformMatrix.setScale(-1.0f, 1.0f); | |
break; | |
case ExifInterface.ORIENTATION_FLIP_VERTICAL: | |
transformMatrix.setScale(1.0f, -1.0f); | |
break; | |
default: | |
bitmapDst = bitmapSrc; | |
break; | |
} | |
if (!transformMatrix.isIdentity()) { | |
bitmapDst = Bitmap.createBitmap(bitmapSrc, 0, 0, bitmapSrc.getWidth(), bitmapSrc.getHeight(), | |
transformMatrix, true); | |
bitmapSrc.recycle(); | |
} | |
return bitmapDst; | |
} | |
public static Bitmap getFixedWidthBitmap(String jsImgPath, int fixedWidth) { | |
if (TextUtils.isEmpty(jsImgPath) || !new File(jsImgPath).exists() || fixedWidth <= 0) { | |
return null; | |
} | |
try { | |
int realFixedWidth; | |
int exifOrientation = readPicOrientation(jsImgPath); | |
switch (exifOrientation) { | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
case ExifInterface.ORIENTATION_TRANSPOSE: | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
case ExifInterface.ORIENTATION_TRANSVERSE: { | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(jsImgPath, options); | |
if (options.outWidth < 1 || options.outHeight < 1) { | |
return null; | |
} | |
realFixedWidth = Math.round((float) fixedWidth * options.outWidth / options.outHeight); | |
} | |
break; | |
default: { | |
realFixedWidth = fixedWidth; | |
} | |
break; | |
} | |
Bitmap bitmap = resampleImage(jsImgPath, realFixedWidth); | |
bitmap = rotateFlipBitmap(bitmap, exifOrientation); | |
bitmap = scaleImage(bitmap, fixedWidth); | |
return bitmap; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static Bitmap resampleImage(String jsImgPath, int resampleWidth) { | |
if (TextUtils.isEmpty(jsImgPath) || !new File(jsImgPath).exists() || resampleWidth <= 0) { | |
return null; | |
} | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(jsImgPath, options); | |
options.inJustDecodeBounds = false; | |
if (0 == options.outWidth || 0 == options.outHeight) { | |
return null; | |
} | |
int w = options.outWidth; | |
int beWidth = w / resampleWidth; | |
if (beWidth <= 0) { | |
beWidth = 1; | |
} | |
options.inSampleSize = beWidth; | |
Bitmap bitmap = BitmapFactory.decodeFile(jsImgPath, options); | |
return bitmap; | |
} | |
public static Bitmap scaleImage(Bitmap bitmap, int fixedWidth) { | |
if (null == bitmap || fixedWidth <= 0) { | |
return bitmap; | |
} | |
if (bitmap.getWidth() <= fixedWidth || 0 == bitmap.getWidth()) { | |
return bitmap; | |
} | |
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, fixedWidth, fixedWidth * bitmap.getHeight() / bitmap.getWidth(), true); | |
if (resizedBitmap != bitmap) { | |
if (!bitmap.isRecycled()) { | |
bitmap.recycle(); | |
} | |
bitmap = resizedBitmap; | |
System.gc(); | |
} | |
return bitmap; | |
} | |
public static Bitmap rotateImage(final Bitmap bitmap, int orientation) { | |
if (bitmap == null) { | |
return null; | |
} | |
Bitmap rotatedBitmap = bitmap; | |
try { | |
if (orientation != 0) { | |
Matrix matrix = new Matrix(); | |
matrix.postRotate(orientation); | |
rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), | |
bitmap.getHeight(), matrix, true); | |
if (!bitmap.isRecycled()) { | |
bitmap.recycle(); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return rotatedBitmap; | |
} | |
public static int readPicDegree(String path) { | |
if (TextUtils.isEmpty(path)) | |
return 0; | |
int degree = 0; | |
try { | |
ExifInterface exifInterface = new ExifInterface(path); | |
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); | |
switch (orientation) { | |
case ExifInterface.ORIENTATION_ROTATE_90: | |
degree = 90; | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_180: | |
degree = 180; | |
break; | |
case ExifInterface.ORIENTATION_ROTATE_270: | |
degree = 270; | |
break; | |
default: | |
break; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return degree; | |
} | |
public static File saveBitmapCompressJPG(Bitmap bitmap, File dir, String fileName, int quality) throws IOException { | |
if (dir == null) { | |
return null; | |
} | |
if (!dir.exists()) { | |
dir.mkdirs(); | |
} | |
File saveFile = new File(dir, fileName); | |
FileOutputStream fos = new FileOutputStream(saveFile); | |
int compressQuality = 100; | |
if (quality >= 0 && quality <= 100) { | |
compressQuality = quality; | |
} | |
if (bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, fos)) { | |
fos.flush(); | |
fos.close(); | |
return saveFile; | |
} | |
return null; | |
} | |
public static InputStream getStreamFromCamera(Context ctx) throws Throwable { | |
InputStream inputStream = null; | |
File imgFile = getCaptureTempFile(); | |
if (imgFile.exists()) { | |
ContentResolver resolver = ctx.getContentResolver(); | |
inputStream = resolver.openInputStream(Uri.fromFile(imgFile)); | |
} | |
return inputStream; | |
} | |
public static InputStream getStreamFromAlbum(Intent data, Context ctx) | |
throws Throwable { | |
if (data == null) { | |
return null; | |
} | |
Uri uri = data.getData(); | |
Bitmap bitmap = null; | |
ContentResolver resolver = ctx.getContentResolver(); | |
if (null != uri) { | |
return resolver.openInputStream(Uri.parse(uri.toString())); | |
} else { | |
if (null != data.getExtras()) { | |
bitmap = data.getExtras().getParcelable("data"); | |
} | |
} | |
InputStream is = null; | |
if (null != bitmap) { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); | |
is = new ByteArrayInputStream(baos.toByteArray()); | |
bitmap.recycle(); | |
} | |
return is; | |
} | |
public static File getCaptureTempFile() { | |
return new File(getDefaultCacheFileDir(), TMP_CAPTURE_FILE_NAME); | |
} | |
public static File getDefaultCacheFileDir() { | |
File file = new File(Environment.getExternalStorageDirectory().toString() | |
+ CACHE_FILE_DIR + "/"); | |
if (!file.exists()) { | |
file.mkdir(); | |
} | |
return file; | |
} | |
public static File generateTempCaptureFile() { | |
String status = Environment.getExternalStorageState(); | |
if (!status.equals(Environment.MEDIA_MOUNTED)) { | |
return null; | |
} | |
File file = getCaptureTempFile(); | |
file.mkdirs(); | |
if (file.exists()) { | |
file.delete(); | |
} | |
return file; | |
} | |
public static boolean deleteTempCaptureFile() { | |
File file = getCaptureTempFile(); | |
if (file.exists()) { | |
file.delete(); | |
return true; | |
} | |
return false; | |
} | |
public static Bitmap getBitmapFromAlbum(Intent data, BitmapFactory.Options opts, Context ctx) | |
throws Throwable { | |
ContentResolver resolver = ctx.getContentResolver(); | |
if (data == null) { | |
return null; | |
} | |
Bitmap bitmap = (Bitmap) data.getExtras().get("data"); | |
return bitmap; | |
} | |
public static Bitmap getBitmapFromCamera(Intent data, BitmapFactory.Options opts, Context ctx) | |
throws Throwable { | |
if (data == null) { | |
return null; | |
} | |
Uri uri = data.getData(); | |
Bitmap bitmap; | |
if (null != uri) { | |
ContentResolver resolver = ctx.getContentResolver(); | |
byte[] content = readStream(resolver.openInputStream(Uri.parse(uri | |
.toString()))); | |
bitmap = getPicFromBytes(content, opts); | |
} else { | |
bitmap = data.getExtras().getParcelable("data"); | |
} | |
return bitmap; | |
} | |
public static byte[] readStream(InputStream inStream) throws Exception { | |
byte[] buffer = new byte[1024]; | |
int len = -1; | |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | |
while ((len = inStream.read(buffer)) != -1) { | |
outStream.write(buffer, 0, len); | |
} | |
byte[] data = outStream.toByteArray(); | |
outStream.close(); | |
inStream.close(); | |
return data; | |
} | |
private static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) { | |
if (bytes != null) { | |
opts.inSampleSize = 4; | |
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment