/**
*调用系统相机拍照工具类
*@authoryao
*
*/
publicclassCaremaUtil{
privatestaticStringstrImgPath="";//照片的绝对路径
privatestaticStringimgPath="";//照片所在文件夹路径
//保存的拍照文件
privatestaticFileout;
/**
*相机照相并返回图片名字
*/
publicstaticStringcameraMethod(Activityactivity,intrequestCode){
//实例化拍照的Intent
IntentimageCaptureIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
//设置图片存放的路径,Environment.getExternalStorageDirectory()得到SD卡的根目录
//先验证手机是否有sdcard
Stringstatus=Environment.getExternalStorageState();
if((Environment.MEDIA_MOUNTED).equals(status)){
strImgPath=getImgPath();
//本地保存原图
Filefile=newFile(strImgPath);
Uriu=Uri.fromFile(file);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT,u);
//启动ACITIVITY
activity.startActivityForResult(imageCaptureIntent,requestCode);
LogUtil.log("imageName---"+getImageName());
}else{
Toast.makeText(activity,"没有储存卡",0).show();
}
returngetImageName();
}
/**
*返回图片的绝对路径
*
*@return
*/
publicstaticStringgetImgPath(){
imgPath=getImagePath();//存放照片的文件夹
LogUtil.log("图片存放的路径-----"+strImgPath);
StringimageName=getImageName();
LogUtil.log("图片全名路径---"+imageName);
//检查存放的路径是否存在,如果不存在则创建目录
try{
out=newFile(imgPath);
if(!out.exists()){
out.mkdirs();
}
//在此目录下创建文件
out=newFile(imgPath,imageName);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
//该照片的绝对路径
strImgPath=imgPath+imageName;
returnstrImgPath;
}
/**
*得到图片所在的文件夹的路径
*
*@return
*/
publicstaticStringgetImagePath(){
returnEnvironment.getExternalStorageDirectory().getAbsolutePath()+"//"
+SoftApplication.softApplication.getString(R.string.app_name)+"/approvalPic/";
}
/**
*得到图片的名字
*
*@return
*/
publicstaticStringgetImageName(){
//给相片命名
StringimageName=newSimpleDateFormat("yyyyMMddHHmmss").format(newDate())+".jpg";//照片命名
returnimageName;
}
/**
*显示图片并得到图片字节字符串
*
*@paramresultCode
*@paramdata
*/
publicstaticStringshowPic(Activityactivity,Intentdata,ImageViewimageView){
StringiamgeStr=newString();
Filefile=newFile(strImgPath);
try{
Uriuri=Uri.fromFile(file);
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
//简单的压缩
BitmapFactory.decodeStream(activity.getContentResolver().openInputStream(uri),null,options);
options.inSampleSize=4;
options.inJustDecodeBounds=false;//压缩完后便可以将inJustDecodeBounds设置为false
//把流转化为Bitmap图片
Bitmapbitmap=BitmapFactory.decodeStream(activity.getContentResolver().openInputStream(uri),null,
options);
android.provider.MediaStore.Images.Media.insertImage(activity.getContentResolver(),bitmap,null,null);
activity.sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,uri));
//得到图片的旋转角度
intdegree=getBitmapDegree(strImgPath);
//根据旋转角度,生成旋转矩阵
Matrixmatrix=newMatrix();
matrix.postRotate(degree);
BitmapreturnBm=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
imageView.setImageBitmap(returnBm);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
press(pressFormat.PNG,100,baos);
byte[]bitmapByte=baos.toByteArray();
iamgeStr=StringUtil.encodeStr(bitmapByte);
LogUtil.log("iamgeStr",Log.ERROR,iamgeStr);
//LogUtil.log("iamgeStr1",Log.ERROR,imageStr1);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
returniamgeStr;
}
/**
*清除本地拍照缓存
*/
publicstaticvoidclearCacheImage(){
Filefilecache=newFile(getImagePath());
LogUtil.log("filecache---"+filecache.getPath());
if(filecache!=null&&filecache.listFiles()!=null){
for(Filefile:filecache.listFiles()){
if(file.isFile()){
file.delete();//删除所有文件
LogUtil.log("删除所有文件");
}
}
}
}
/**
*获取原始图片的角度(解决三星手机拍照后图片是横着的问题)
*@parampath图片的绝对路径
*@return原始图片的角度
*/
publicstaticintgetBitmapDegree(Stringpath){
intdegree=0;
try{
//从指定路径下读取图片,并获取其EXIF信息
ExifInterfaceexifInterface=newExifInterface(path);
//获取图片的旋转信息
intorientation=exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Log.e("jxf","orientation"+orientation);
switch(orientation){
caseExifInterface.ORIENTATION_ROTATE_90:
degree=90;
break;
caseExifInterface.ORIENTATION_ROTATE_180:
degree=180;
break;
caseExifInterface.ORIENTATION_ROTATE_270:
degree=270;
break;
}
}catch(IOExceptione){
e.printStackTrace();
}
returndegree;
}
}