700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android调用系统照相机拍照 并压缩保存在本地

android调用系统照相机拍照 并压缩保存在本地

时间:2021-01-20 17:08:32

相关推荐

android调用系统照相机拍照 并压缩保存在本地

1.首先拍照和保存文件肯定就需要申请权限

<!-- 往SDCard写入数据权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!-- 在SDCard中创建与删除文件权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!--照相机权限--><uses-permission android:name="android.permission.CAMERA"/>

2.在布局中添加一个按钮和一个ImageView用来显示图片缩略图,布局如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:orientation="vertical"tools:context=".mycamera.MainActivity"><Button android:id="@+id/camera"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="调用系统照相机拍照"/><ImageView android:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

3.点击button调用系统照相机进行拍照,button监听事件如下:

//调用系统相机File dir = new File(Environment.getExternalStorageDirectory(), "myimage");//在sd下创建文件夹myimage;Environment.getExternalStorageDirectory()得到SD卡路径文件if (!dir.exists()) { //exists()判断文件是否存在,不存在则创建文件dir.mkdirs();}SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式在android中,创建文件时,文件名中不能包含“:”冒号String filename = df.format(new Date());currentImageFile = new File(dir, filename + ".jpg");if (!currentImageFile.exists()) {currentImageFile.createNewFile();}Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentImageFile));startActivityForResult(openCameraIntent, ACTION_TAKE_PHOTO);

4.重写onActivityResult()方法,在该方法中来处理返回的数据。

if (resultCode == RESULT_OK && requestCode == ACTION_TAKE_PHOTO) {String sdStatus = Environment.getExternalStorageState();if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用Log.i("TestFile", "SD card is not avaiable/writeable right now.");return;}//原图String filePath = file.getAbsolutePath();Bitmap bitmap = BitmapFactory.decodeFile(filePath);//利用Bitmap对象创建缩略图Bitmap showbitmap = ThumbnailUtils.extractThumbnail(bitmap, 250, 250);iv_imageView.setImageBitmap(showbitmap);

此时我们拍照完成之后,照片会保存在SD下的myimage文件夹下,但是图片没有进行压缩。图片大小大约1M多(这和手机有关),并且缩略图并没有保存只是显示而已。

效果如图:

4.对图片进行压缩,压缩方法有按比例大小压缩按质量压缩

//采样率压缩(根据路径获取图片并压缩):public Bitmap getSmallBitmap(File file, int reqWidth, int reqHeight) {try {String filePath = file.getAbsolutePath();BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;//开始读入图片,此时把options.inJustDecodeBounds 设回true了BitmapFactory.decodeFile(filePath, options);//此时返回bm为空options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);//设置缩放比例 数值越高,图片像素越低options.inJustDecodeBounds = false;//重新读入图片,注意此时把options.inJustDecodeBounds 设回false了Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);//压缩好比例大小后不进行质量压缩BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(currentImageFile));press(pressFormat.JPEG, 100, bos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到bos中//压缩好比例大小后再进行质量压缩//compressImage(bitmap,filePath);return bitmap;} catch (Exception e) {Log.d("wzc", "类:" + this.getClass().getName() + " 方法:" + Thread.currentThread().getStackTrace()[0].getMethodName() + " 异常 " + e);return null;}}/*** 计算图片的缩放值** @param options* @param reqWidth* @param reqHeight* @return*/public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {try {int height = options.outHeight;int width = options.outWidth;int inSampleSize = 1; //1表示不缩放if (height > reqHeight || width > reqWidth) {int heightRatio = Math.round((float) height / (float) reqHeight);int widthRatio = Math.round((float) width / (float) reqWidth);inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;}return inSampleSize;} catch (Exception e) {Log.d("wzc", "类:" + this.getClass().getName() + " 方法:" + Thread.currentThread().getStackTrace()[0].getMethodName() + " 异常 " + e);return 1;}}// 质量压缩法:private Bitmap compressImage(Bitmap image, String filepath) {try {ByteArrayOutputStream baos = new ByteArrayOutputStream();press(pressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中int options = 100;while (baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩baos.reset();//重置baos即清空baosoptions -= 10;//每次都减少press(pressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中}//压缩好后写入文件中FileOutputStream fos = new FileOutputStream(filepath);fos.write(baos.toByteArray());fos.flush();fos.close();return image;} catch (IOException e) {e.printStackTrace();return null;}}

下面是我进行压缩前后的对比:

第一张是没有进行压缩的图片,第二张是进行按比例压缩的图片,最后一张是按比例压缩后在进行质量压缩后的照片。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。