android拍照简书,android系统拍照完整流程

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;

}

基本处理操作就这么多,仅供参考。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值