package com.jqyd.camera;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.jqyd.app.MyApp;
import com.jqyd.camera.UIUtil;
import com.jqyd.camera.AccessoryDTO;
import com.jqyd.manager.R;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.son.Ddxq;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Toast;
public class PhotoUtil {
public static final String SDCARD_ROOT_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath();// 路径
public static final String SAVE_PATH_IN_SDCARD = "/jqgj/Images/"; // 图片及其他数据保存文件夹
public static String PhotoName;
public static final int REQUEST_Photograph = 101;
public static final int REQUEST_ChoosePicture = 102;
public static final int REQUEST_AddChoosePicture = 103;
public static List<AccessoryDTO> photos = new ArrayList<AccessoryDTO>();
public static AccessoryDTO photo = new AccessoryDTO();
public static boolean isPhotoing = false;
/**
* 返回图片路径
*
* @return
*/
public static String getPhotoPath() {
return SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD + PhotoName;
}
/**
* 根据时间生成照片文件名称
*
* @return
*/
public static String createPhotoName() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
DecimalFormat MonthFormat = new DecimalFormat("00");
String h = MonthFormat.format(cal.get(Calendar.HOUR_OF_DAY));
String m = MonthFormat.format(cal.get(Calendar.MINUTE));
String s = MonthFormat.format(cal.get(Calendar.SECOND));
StringBuffer b = new StringBuffer();
b.append(df.format(cal.getTime()) + "_");
b.append(h + m + s + ".jpg");
return b.toString();
}
/**
* 调用系统相机
*
* @param context
*/
public static void startSysCamera(Activity context) {
if (PhotoUtil.isHasSdcard()) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 定义图片名称
PhotoName = createPhotoName();
File f = new File(PhotoUtil.SDCARD_ROOT_PATH
+ PhotoUtil.SAVE_PATH_IN_SDCARD);
if (!f.exists()) {
f.mkdirs();
}
if (PhotoUtil.photos.size() < 3) {
try {
Optsharepre_interface share_obj = new Optsharepre_interface(
context);
share_obj.editPres("istakephoto", "1");
AccessoryDTO photo = new AccessoryDTO();
photo.setPath(PhotoUtil.getPhotoPath());
photo.setName(PhotoUtil.PhotoName);
System.out.println("PhotoUtil.PhotoName----"
+ photo.getName());
photo.setType(AccessoryDTO.AccessoryType.image.getCode());
MyApp myApp = (MyApp) context.getApplication();
myApp.setPhoto(photo);
intent.putExtra(
MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(SDCARD_ROOT_PATH+ SAVE_PATH_IN_SDCARD, PhotoName)));
context.startActivityForResult(intent, REQUEST_Photograph);
} catch (Exception e) {
Toast.makeText(context, "调用系统相机失败", Toast.LENGTH_LONG).show();
}
} else {
UIUtil.alertMsg(context, "最多拍摄3张照片");
}
} else {
UIUtil.alertMsg(context, "请插入SD卡");
}
}
/**
* 调用自定义相机
*
* @param context
*/
public static void startSelfCamera(Activity context) {
if (PhotoUtil.isHasSdcard()) {
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// intent.setClass(context,CameraActivity.class);
context.startActivityForResult(intent, REQUEST_Photograph);
// 实现左移动画效果
context.overridePendingTransition(R.anim.in_from_right,
R.anim.out_to_left);
} else {
UIUtil.alertMsg(context, "请插入SD卡");
}
}
/**
* 打开系统相册选取照片
*
* @param context
*/
public static void chooseSysPhoto(Activity context) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
context.startActivityForResult(intent, REQUEST_ChoosePicture);
}
/**
* 打开添加照片界面,可以拍照或选择手机内部已存照片
*
* @param context
*/
public static void AddOrChoosePhoto(Activity context) {
Intent intent = new Intent();
intent.setClass(context, AddPhotoActivity.class);
context.startActivityForResult(intent, REQUEST_AddChoosePicture);
}
/**
* 检查存储卡是否插入
*
* @return
*/
public static boolean isHasSdcard() {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
public static void Clear() {
PhotoName = "";
if (photos != null) {
photos.clear();
photos = null;
}
}
/**
* byte[] → Bitmap
*
* @param b
* @return
*/
public static Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
/**
* Bitmap → byte[]
*
* @param bm
* @param quality
* 100表示不压缩, 0-100之前表示压缩,数值越小,压缩程度越高
* @return
*/
public static byte[] Bitmap2Bytes(Bitmap bm, int quality) {
if (quality <= 0 || quality > 100) {
quality = 100;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, quality, baos);
return baos.toByteArray();
}
/**
* 根据图片路径读取图片,并进行缩放压缩 ,返回图片字节数组
*
* @param photpath
* @return 压缩后图片字节数组
*/
public static byte[] PhotoToBytes(String photpath) {
byte[] res = null;
File f = new File(photpath);
if (f.exists()) {
Bitmap bm = ReadBitmap(photpath, 600, 800);
res = Bitmap2Bytes(bm, 80);
bm.recycle();
}
return res;
}
/**
* 计算图片缩放比例
*
* @param options
* 原图
* @param reqWidth
* 希望宽度
* @param reqHeight
* 希望高度
* @return 缩放比例 1表示宽度和高度不缩放,为2表示压缩后的宽度与高度为原来的1/2
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
int heightRatio = Math.round((float) height / (float) reqHeight);
int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 压缩读取图片,这样不会内存溢出
*
* @param photpath
* @return 压缩后的bitmap
*/
public static Bitmap ReadBitmap(String photpath, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
// 根据图片路径读取图片,获取图片大小,但不读入到内存
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(photpath, options); // 此时返回bm为空
// 计算缩放比例
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// 重新读入图片,并在内存中生成压缩后的bitmap
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(photpath, options);
return bitmap;
}
/**
* 根据图片文件Uri得到其真实路径;
*
* @param context
* @param uri
* @return
*/
public static String getPhotoPath(Activity context, Uri uri) {
// 获取录音文件真实路径
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(uri, filePathColumn,
null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
}
/**
* 放大缩小图片
*
* @param bm
* 图片源
* @param newWidth
* 类型为dp或dip
* @param newHeight
* 类型为dp或dip
* @return
*/
public static Bitmap zoomBitmap(Context context, Bitmap bm, int newWidth,
int newHeight) {
// 图片源
// Bitmap bm = BitmapFactory.decodeStream(getResources()
// .openRawResource(id));
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 设置想要的大小
int newWidth1 = UIUtil.dip2px(context, newWidth);
int newHeight1 = UIUtil.dip2px(context, newHeight);
// 计算缩放比例
float scaleWidth = ((float) newWidth1) / width;
float scaleHeight = ((float) newHeight1) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}
/**
* 根据图片实际角度,旋转图片
*
* @param photpath
* 图片实际路径
* @param bm
* 要旋转的图片
* @return
*/
public static Bitmap RotateBitmap(String photpath, Bitmap bm) {
ExifInterface exifInterface = null;
try {
exifInterface = new ExifInterface(photpath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
int tag = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
-1);
int degree = 0;
if (tag == ExifInterface.ORIENTATION_ROTATE_90) {
degree = 90;
} else if (tag == ExifInterface.ORIENTATION_ROTATE_180) {
degree = 180;
} else if (tag == ExifInterface.ORIENTATION_ROTATE_270) {
degree = 270;
}
return RotateBitmap(degree, bm);
}
/**
* 根据输入旋转角度,旋转图片
*
* @param degree
* 旋转角度
* @param bm
* 要旋转的图片
* @return
*/
public static Bitmap RotateBitmap(int degree, Bitmap bm) {
Matrix m = new Matrix();
if (degree != 0 && bm != null) {
m.setRotate(degree, (float) bm.getWidth() / 2,
(float) bm.getHeight() / 2);
}
// 旋转图片
Bitmap bmp = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
return bmp;
}
/**
* 根据图片数据项显示图片
*
* @param context
* @param Color_Display
* @param photo
* @param width
* 类型为dp或dip
* @param height
* 类型为dp或dip
*/
public static void showPicture(Context context, ImageView Color_Display,
AccessoryDTO photo, int width, int height) {
// 显示图片
if (!TextUtils.isEmpty(photo.getPath())) {
// 压缩读取图片,否走读取大图时会内存溢出
Bitmap bmp1 = ReadBitmap(photo.getPath(), 150, 200);
// 旋转图片
Bitmap bmp2 = RotateBitmap(photo.getPath(), bmp1);
// 缩放图片
Bitmap bmp3 = zoomBitmap(context, bmp2, width, height);
// 显示图片
Color_Display.setImageBitmap(bmp3);
// bmp3.recycle();
bmp2.recycle();
bmp1.recycle();
} else {
Color_Display.setImageBitmap(null);
Color_Display.setBackgroundDrawable(context.getResources()
.getDrawable(R.drawable.bg_transparent_tile));
}
}
}