1、拍照相片存储的临时文件
File savePath=new File(Environment.getExternalStorageDirectory(),"/temp/temp.jpg");
2、开启系统相机
Intent cameraIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri imageUri = Uri.fromFile(savePath);
// 指定照片保存路径(SD卡),temp.jpg为一个临时文件,每次拍照后这个图片都会被替换
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, Constant.PHOTO_GRAPH);
3、当拍照完毕后系统会调用onActivityResult
@Override
public void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case Constant.PHOTO_GRAPH:
if(RESULT_CANCELED!= resultCode) {
//根据savePath.getAbsolutePath()绝对路径获取当前拍摄的图片去做相关逻辑操作
//保存图片到系统内容提供器
url[0] = MediaStore.Images.Media.insertImage(appContext.getContentResolver(),
picPath,null,null);
}
}
break;
4、创建本地图片存放路径
private String createPicPath(String path) {
File dirFile =new File(Environment.getExternalStorageDirectory().getPath()
+ path);
if(!dirFile.exists()) {
dirFile.mkdirs();
}
// 格式化时间
String dataTake =new SimpleDateFormat(
"yyyyMMddHHmmssSSS").format(newDate());
return dirFile.getPath() + File.separator+ dataTake +".jpg";
}
4、保存本地图片
public boolean saveImage(String path, String spath) {
BufferedOutputStream bos =null;
try{
bos =new BufferedOutputStream(
new FileOutputStream(spath,false));
Bitmap photo = getBitmap(path);
photo.compress(Bitmap.CompressFormat.JPEG,80, bos);
bos.flush();
}catch(Exception e) {
e.printStackTrace();
return false;
}finally{
try{
if(bos !=null)
bos.close();
}catch(IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
5、缩放图片,压缩质量
public boolean saveImage(String path, String spath) {
BufferedOutputStream bos =null;
try{
bos =new BufferedOutputStream(
newFileOutputStream(spath,false));
Bitmap photo = getBitmap(path);
photo.compress(Bitmap.CompressFormat.JPEG,80, bos);
bos.flush();
}catch(Exception e) {
e.printStackTrace();
return false;
}finally{
try{
if(bos !=null)
bos.close();
}catch(IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
6、压缩比例
public Bitmap getBitmap(String filePath) {
final BitmapFactory.Options options =newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize=calculateInSampleSize1(options,480,800);
options.inJustDecodeBounds=false;
Bitmap bitmap = toRotationBitmap(BitmapFactory.decodeFile(filePath, options), getExifOrientation(filePath));
returnbitmap;
}
7、旋转图片
private Bitmap toRotationBitmap(Bitmap bitmap,final introtation) {
if(rotation ==0)
return bitmap;
Matrix matrix =new Matrix();
matrix.postRotate(rotation);
int height = bitmap.getHeight();
int width = bitmap.getWidth();
float scale =1f;
// 防止内存溢出,先添加一个基本判断,根据测试情况更改
while(height >2200) {
height /=2;
width /=2;
scale *=0.5f;
}
matrix.postScale(scale, scale);
int neHeight = height;
int neWidth = width;
if(rotation ==90|| rotation ==270) {
neHeight = width;
neWidth = height;
}
Bitmap canvasBitmap = Bitmap.createBitmap(neWidth, neHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas =newCanvas(canvasBitmap);
float transX = height;
float transY =0;
if(rotation ==90) {
transX = height;
transY =0;
}else if(rotation ==180) {
transX = width;
transY = height;
}else if(rotation ==270) {
transX =0;
transY = width;
}else{
transX =0;
transY =0;
}
matrix.postTranslate(transX, transY);
canvas.drawBitmap(bitmap, matrix,newPaint());
if(bitmap !=null) {
bitmap.recycle();
bitmap =null;
System.gc();
}
return canvasBitmap;
}
8、获取拍摄角度
public int getExifOrientation(String path) {
String img_path = path;
System.out.println("图片位置"+ img_path);
int degree =0;
ExifInterface exif =null;
try{
exif =new ExifInterface(img_path);
}catch(IOException e) {
e.printStackTrace();
}
if(exif !=null) {
intorientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if(orientation != -1) {
// We only recognize a subset of orientation tag values.
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;
}
}
}
return degree;
}
基本处理操作就这么多,仅供参考。