Android调用系统摄像头拍照并剪裁压缩
2014-12-26 15:12
453 查看
由于业务需要写了一个Android手机拍照的功能Demo,同时实现了图片剪裁和图片压缩。以下是源码
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
package com.klp.demo_025; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private Button button; private File file; private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); button = (Button) findViewById(R.id.button1); file = new File(this.getExternalFilesDir(null), "image.jpg"); uri = Uri.fromFile(file); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, 2); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == 2) { startPhotoZoom(uri); } else { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), options); // 压缩图片 // bitmap = compressImage(bitmap,500); if (bitmap != null) { // 显示图片 iv.setImageBitmap(bitmap); // 保存图片 FileOutputStream fos = null; fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } } catch (Exception e) { // TODO: handle exception } } } } /** * 裁剪图片 * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面. intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例. intent.putExtra("aspectY", 1);// x:y=1:1 intent.putExtra("outputX", 200);//图片输出大小 intent.putExtra("outputY", 200); intent.putExtra("output", uri); intent.putExtra("outputFormat", "JPEG");// 返回格式 startActivityForResult(intent, 3); } /** * 将图片image压缩成大小为 size的图片(size表示图片大小,单位是KB) * * @param image * 图片资源 * @param size * 图片大小 * @return Bitmap */ private Bitmap compressImage(Bitmap image, int size) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 while (baos.toByteArray().length / 1024 > size) { // 重置baos即清空baos baos.reset(); // 每次都减少10 options -= 10; // 这里压缩options%,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, options, baos); } // 把压缩后的数据baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把ByteArrayInputStream数据生成图片 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } }下面是布局,很简单
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <ImageView android:id="@+id/imageView1" android:layout_width="200dp" android:layout_height="200dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="64dp" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:text="拍照" /> </RelativeLayout>最后注意,一定要在AndroidManifest.xml添加SD卡访问权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
相关文章推荐
- Android调用系统摄像头拍照并剪裁压缩
- Android 调用手机相册、摄像头拍照及剪裁照片
- Android从相册选择图片和调用系统摄像头拍照
- Android 调用系统相机拍照 2次剪裁. 选取本地相册
- Android——调用系统摄像头拍照的问题
- 调用系统摄像头拍照,对拍照后的图片进行裁剪和压缩处理,并显示在imageview上面
- [置顶] Android 7.0 调用系统拍照,相册,剪裁,修复拍照图片旋转问题
- Android客户端之“微服私访”App的系统学习(八)调用系统摄像头拍照并管理照片并使用Okhttp上传文件至后台
- android 调用系统照相机拍照后剪裁
- android调用系统照相机拍照,并压缩保存在本地
- Android拍照调用系统相册仿微信封装总结,治疗各种崩溃,图片横竖问题压缩等问题。-更新2016-12-21
- Android调用系统摄像头拍照并显示在ImageView上
- MVP模式的Android 调用系统拍照,相册,剪裁,适配到7.0,修复拍照图片旋转问题
- Android学习之调用系统照相机拍照并保存(照片会被压缩)
- 主题:android调用系统摄像头拍照图片和视频
- Android拍照调用系统相册仿微信封装总结,治疗各种崩溃,图片横竖问题压缩等问题。
- Android进阶——多线程间的通信之调用系统标准摄像头自动对焦并自动完成隐蔽拍照
- Android 调用系统摄像头拍照保存并且更新到图库
- android 调用系统摄像头拍照及截剪
- 调用Android系统摄像头进行拍照以及从图库选择图片