android调用系统拍照那些事

一身转战三千里,一剑曾当百万师。这篇文章主要讲述android调用系统拍照那些事相关的知识,希望能为你提供帮助。
小时候都知道每天写日记是个好习惯,慢慢发现自己忘记了这些习惯,好久没有给这干枯的博客添加一点新意了,这回也冒出个小芽来刷新一下博客。
今天写的一点也不复杂,就是回顾一些老的知识而已,也算是记一个笔记,好了闲话不多说了,开始今天的主题吧。
关于拍照,这里不是自己实现拍照,是调用系统拍照,很简单的,可是有些时候我也遇到一个问题,就是我没有主动压缩,系统却自动帮我压缩了,可是我需要这些高清的图片,解决方式网上也有说,但是我做的是自己的笔记,所以也不在乎赘余,最起码我是经过验证后,才写我笔记的。
下面是系统默认压缩图片的调用方式

1private static final int TAKE_PICTURE = 0x000001; 2public void photo() { 3Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 4startActivityForResult(openCameraIntent, TAKE_PICTURE); 5}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { switch (requestCode) { case TAKE_PICTURE: if (& & resultCode == RESULT_OK) { String fileName = String.valueOf(System.currentTimeMillis()); Bitmap bm = null; ContentResolver resolver = getContentResolver(); if (originalUri == null) { bm = (Bitmap) intent.getExtras().get("data"); //小米5测试发现好像走这里,华为G521 android4.3老机器也走这里 } else { try { bm = BitmapFactory.decodeStream(resolver.openInputStream(originalUri)); LogUtil.e(TAG, "originalBitmap photo width=" + bm.getWidth()); bm = comp(bm); //这里是压缩至于压缩方法,就放下面提供了 } catch (FileNotFoundException e) { e.printStackTrace(); } }
//剩下就自己操作bitmap了比如下面的保存
FileUtils.saveBitmap(bm, fileName);
//........
}
break;

this.mLoginHelper.onActivityResult(requestCode, resultCode, intent);

}

上面的是常用的调用方式,缺点就是图片会被系统自己压缩。
也不兜圈子,下面的方法就是保存原图的方式。
1private static final int TAKE_PICTURE = 0x000001; 2private String fileName; 3 4public void photo() { 5Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 6fileName = String.valueOf(System.currentTimeMillis()); 7File photoFile = FileUtils.createPic(fileName); 8openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 9startActivityForResult(openCameraIntent, TAKE_PICTURE); 10}

返回调用
1 @Override 2protected void onActivityResult(int requestCode, int resultCode, Intent intent) {36switch (requestCode) { 37case TAKE_PICTURE: 38if (Bimp.tempSelectBitmap.size() < 3 & & resultCode == RESULT_OK) { 44Uri originalUri = null; 45File f=FileUtils.getPic(fileName); 46try { 47originalUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),f.getAbsolutePath(), null, null)); 48} catch (FileNotFoundException e) { 49e.printStackTrace(); 50} 52Bitmap bm = null; 53ContentResolver resolver = getContentResolver(); 56if (originalUri == null) { 58bm = (Bitmap) intent.getExtras().get("data"); 59} else { 60try { 61bm = BitmapFactory.decodeStream(resolver.openInputStream(originalUri)); 63LogUtil.e(TAG, "originalBitmap photo width=" + bm.getWidth()); 65bm = comp(bm); 66} catch (FileNotFoundException e) { 67e.printStackTrace(); 68} 69 70} 71FileUtils.saveBitmap(bm, fileName); 72 73//继续操作78 79} 80break; 81} 82this.mLoginHelper.onActivityResult(requestCode, resultCode, intent); 83}

  下面是保存方法:
public static void saveBitmap(Bitmap bm, String picName) { try { if (!isFileExist("")) { File tempf = createSDDir(""); } File f = new File(SDPATH, picName + ".JPEG"); if (f.exists()) { f.delete(); } FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

下面是压缩方法:
private Bitmap comp(Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); if (baos.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset(); // 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, baos); // 这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = 1000f; // 这里设置高度为800f float ww = 1000f; // 这里设置宽度为480f // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1; // be=1表示不缩放 if (w > h & & w > ww) {// 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h & & h > hh) {// 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be < = 0) be = 1; newOpts.inSampleSize = be; // 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); return compressImage(bitmap); // 压缩好比例大小后再进行质量压缩 }private Bitmap compressImage(Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; //while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 while (baos.toByteArray().length / 1024 > 2048) { // 循环判断如果压缩后图片是否大于1024kb,大于继续压缩 baos.reset(); // 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos); // 这里压缩options%,把压缩后的数据存放到baos中 options -= 10; // 每次都减少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); // 把ByteArrayInputStream数据生成图片 return bitmap; }

【android调用系统拍照那些事】 

    推荐阅读