调用系统的Camera和相册/压缩照片
来源:互联网 发布:奥卡福 知乎 编辑:程序博客网 时间:2024/05/22 15:36
调用系统的Camera
1.这只是简单的调用一下摄像头,然后拍一张照片。这个命令是基于Intent的,需要在setAction中添加调用摄像头的语句,然后利用Intent.putExtra将得到的照片存放到file里
最后复写onActivityResult,将拍下来的照片显示到ImageView上
2.需要权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
3.有的手机需要不能将所照的图片显示在ImageView上,是因为所照的照片比较大,需要将其压缩,就可以正常显示了,不过这里的压缩方法是在原文件的基础上进行的压缩,压缩后的文件会替换掉原文件
调用手机相册
1.从相册中选择图片,需要创建一个Intent对象,将他的action设置为Intent.ACTION_GET_CONTENT,然后设置类型为“iamge/*”,最后调用startActivityForResult()方法,就可以打开相册程序选择照片
2.在onActivityResult方法中,利用data来得到所选择的照片,从而将照片显示到ImageView上去,这里就不能用压缩了,因为这个压缩方法会将原来的图片替换为压缩过后的图片,因此不建议使用该压缩方法
代码实现
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button mBtnCamera; private Button mBtnPicture; private ImageView mImage; private File file; public static final int CAMERA=0x88; public static final int PICTTURE=0x66; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImage = (ImageView) findViewById(R.id.image_camera); mBtnCamera = (Button) findViewById(R.id.button_camera); mBtnPicture = (Button) findViewById(R.id.button_picture); mBtnCamera.setOnClickListener(this); mBtnPicture.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_camera: Intent intent=new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//Standard Intent action that can be sent to have the camera application capture an image and return it. file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");//创建一个文件 try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//将拍下来的照片,存放到file路径下 startActivityForResult(intent, CAMERA); break; case R.id.button_picture:// Intent intentPicture=new Intent("android.intent.action.GET_CONTENT"); Intent intentPicture=new Intent(Intent.ACTION_GET_CONTENT); intentPicture.setType("image/*"); startActivityForResult(intentPicture,PICTTURE); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //判断requestCode和resultCode是否是符合情况,如果符合就将拍下来的照片显示在ImageView上 if (resultCode==RESULT_OK){ switch (requestCode){ case CAMERA: ImageZip.zipImage(file.getAbsolutePath()); mImage.setImageURI(Uri.fromFile(file)); break; case PICTTURE: Uri uri=data.getData(); mImage.setImageURI(uri); break; } } }}
压缩照片代码
在源文件的基础上进行压缩,替换掉源文件
public class ImageZip { public static void zipImage(String savePath) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(savePath, options); options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(savePath, options); try { FileOutputStream fos = new FileOutputStream(savePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } bitmap.recycle(); bitmap = null; System.gc(); } public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math .sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }}
0 0
- 调用系统的Camera和相册/压缩照片
- Camera调用系统相册
- iOS 上传照片到服务器 获取相册和拍照功能的照片 照片压缩上传
- Android 调用Camera和相册
- 调用系统的相册、相机、闪光灯,可以选择相册的照片
- Camora调用系统的照相机和相册以及图片的压缩
- Android 7.0实际开发中调用系统相机和获取相册照片遇到的坑具备向下兼容
- [置顶] Android 7.0实际开发中调用系统相机和获取相册照片遇到的坑具备向下兼容
- 调用系统的拍照和相册
- 调用系统的相机和相册
- 调用系统的相册和摄像头问题
- work_weipa_调用系统照相机时拍出的照片在系统相册中模糊不清
- 关于调用系统相机以及压缩照片
- 调用系统相册选取照片并使用系统裁剪图片
- 获取系统相册里面的照片
- android中调用系统的相册或者相机拍照并上传照片
- iOS开发:调用系统自带相机以及获取相册照片的功能实现
- Android调用系统相册选择照片并裁剪
- hdu~5025(bfs+状态压缩)
- ios开发-新浪微博-09(下拉菜单封装一)
- 计算机网络学习 :互联网与万维网的区别
- XML解析 JSON解析
- Effective C++——条款43(第7章)
- 调用系统的Camera和相册/压缩照片
- hdu 5023 A Corrupt Mayor's Performance Art(线段树+位运算)
- (新手向)幻想战境DEMO(二)
- Android 二维码扫描 YUV字节数组转换成bitmap
- Java语言的基础语法(二)
- ViewController的生命周期分析和使用
- Android学习笔记之——Spinner
- 类与类之间的关系
- Photoshop的动作