关于Android开发调用系统相机拍照的 一些事

来源:互联网 发布:spring 源码阅读建议 编辑:程序博客网 时间:2024/05/29 21:30

相比大家在开发过程中,开发会遇到调用系统相机出现的各式各样的问题,鉴于本人在开发过程中遇到的问题,想分享一下这些东西,还请大家多多给出意见,下面我就直接阐述问题以及给出方法:

  1. 调用系统相机拍照从oActivityResult回调直接取出数据,转化为bitmap出现图片模糊的现象,这是由于获取的图片只是缩略图,当然你可以获取图片的宽和高就知道了下面就直接上代码,
/**     * 拍摄照片     */    @SuppressLint("SimpleDateFormat")    public void photo() {        String currentTime = getCurrentTime();            photo_name = currentTime+".JPEG";        Intent openCameraIntent = new Intent(                        MediaStore.ACTION_IMAGE_CAPTURE);                openCameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,                        Uri.fromFile(new File(FileUtils.SDPATH + photo_name)));                startActivityForResult(openCameraIntent, TAKE_PICTURE);     }

**名称,并通过MediaStore.EXTRA_OUTPUT来给图片输入指定路径,这样我们就不用从data中取数据了,因为我们知道图片的路径,相比大家不会有什么疑惑。
本Markdow**。

2 .下面展示在OnActivityResult中做处理

BitmapFactory.Options options = new BitmapFactory.Options();
ptions.inJustDecodeBounds = false;
ptions.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(FileUtils.SDPATH
+ photo_name, options);

这样取出来之后你可以对图片进行处理了,当然了,这时候图片如果过大会很容易引起内存溢出,那么解决方法肯定是有的,我们只需要根据图片本身的大小进行相应的处理,比如是设置其分据本身需求进行相应的需求:

`ByteArrayOutputStream os = new ByteArrayOutputStream();
// scale
int options = 100;
// Store the bitmap into output stream(no compress)
bm.compress(Bitmap.CompressFormat.JPEG, options, os);
// Compress by loop
while (os.toByteArray().length / 1024 > 130) {
// Clean up os
os.reset();
// interval 10
options -= 10;
bm.compress(Bitmap.CompressFormat.JPEG, options, os);
}
FileOutputStream fos = null;
try {
if (!isFileExist(“”)) {
File tempf = createSDDir(“”);
}
File f = new File(SDPATH, picName + “.JPEG”);
if (f.exists()) {
f.delete();
}
fos = new FileOutputStream(f);
fos.write(os.toByteArray());
fos.flush();
fos.close();
return f.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
fos.close();
} catch (IOException e) {
Log.e(“IOExcept

ion”,Message(), e);
}
}
}
return “”;`

这里通过图片大小进行适当的压缩来达到期望的小进行适当的压缩来达到期望的需求。
今天先进行到这里,后面会继续写关于这个问题

0 0
原创粉丝点击