public class ImageUtils {
public static final int GET_IMAGE_BY_CAMERA = 5001;
public static final int SYS_INTENT_REQUEST = 5002;
public static String path;
/**
* 调用相机拍照
*/
@SuppressLint("SimpleDateFormat")
public static void cameraPhoto(Activity context, String fileName) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(context, "SD卡不可用!", Toast.LENGTH_SHORT).show();
return;
}
try {
File file = createFile(context, fileName);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
context.startActivityForResult(intent, GET_IMAGE_BY_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 在指定文件夾下面創建文件用来保存文件
*
* @param fileName
* @return
* @throws IOException
*/
private static File createFile(Activity context, String fileName)
throws IOException {
path = Environment.getExternalStorageDirectory().getPath()
+ "/zcImage/";
File fileDir = new File(path);
/** 检测文件夹是否存在,不存在则创建文件夹 **/
if (!fileDir.exists() && !fileDir.isDirectory())
fileDir.mkdirs();
File file = new File(fileDir, fileName);
if (!file.exists()) {
file.createNewFile();
}
return file;
}
/**
* @param data
* 拍照后获取照片
*/
public static void cameraCamera(Activity context, ImageView imageView,
Intent data, String fileName) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
showImgs(context, bitmap, imageView, fileName);
}
/**
* 展示选择的图片
*
* @param bitmap
* @param isSysUp
*/
public static void showImgs(final Activity context, Bitmap bitmap,
ImageView imageView, final String fileName) {
Bitmap _bitmap = compressionBigBitmap(bitmap);
_bitmap = compressImage(_bitmap);
int degree = readPictureDegree(fileName);
if (degree <= 0) {
imageView.setImageBitmap(_bitmap);
} else {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
_bitmap = Bitmap.createBitmap(_bitmap, 0, 0, _bitmap.getWidth(),
_bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(_bitmap);
}
}
/**
* @param bitmap
* @return 压缩后的bitmap
*/
private static Bitmap compressionBigBitmap(Bitmap bitmap) {
Bitmap destBitmap = null;
if (bitmap.getWidth() > 300) {
float scaleValue = (float) (300f / bitmap.getWidth());
Matrix matrix = new Matrix();
matrix.postScale(scaleValue, scaleValue);
destBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
} else {
return bitmap;
}
return destBitmap;
}
/**
* 将bitmap图片压缩
*
* @param image
* @return
*/
private static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
options -= 10;
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
private static int readPictureDegree(String fileName) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(fileName);
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;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 打开系统相册
*/
public static void systemPhoto(Activity context) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
context.startActivityForResult(intent, SYS_INTENT_REQUEST);
}
/**
* 根据图片的URi获取图片的路径
*
* @param uri
* @param activity
* @return
*/
public static String uriToPath(Uri uri,Activity context) {
String[] proj = { MediaStore.Images.Media.DATA };
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(uri, proj,null, null, null);
int actual_image_column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String imageFilePath = cursor.getString(actual_image_column_index);
cursor.close();
return imageFilePath;
}
/**
* 将bitmap对象转化成base64
*
* @param bitmap
* @return
*/
public static String getImgString(Bitmap bitmap) {
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos);
bos.flush();
bos.close();
byte[] imgBytes = bos.toByteArray();
return Base64.encodeToString(imgBytes, Base64.DEFAULT);
} catch (Exception e) {
return null;
} finally {
try {
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 旋转图片 解决三星手机拍照 图片旋转的问题
*
* @param angle
* @param bitmap
* @return Bitmap
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
System.out.println("angle2=" + angle);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}
}