Android 多媒体应用:开启摄像头、查看相册中的照片
来源:互联网 发布:外籍院士 知乎 编辑:程序博客网 时间:2024/06/04 17:58
- 开启摄像头
- 主函数代码
- 布局
- 压缩图片的方法
- 查看相册中照片的代码
- 压缩相册里的原有图片的方法
- 开启摄像头
开启摄像头
开启摄像头是在ACtivity中打开系统自带的摄像头应用。
主函数代码
private Button mButtonStartCamera; private ImageView mImageView; private File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonStartCamera = (Button) findViewById(R.id.button_start_camera); mImageView = (ImageView) findViewById(R.id.imageview); mButtonStartCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机 file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//告诉系统相机将照片保存的位置 startActivityForResult(intent, 0x23);//开始启动 } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode ==0x23) { if (resultCode==RESULT_OK){ //有些手机不能显示出拍出来的图片是因为图片过大,所以在显示之前需要把图片压缩一下——————ImageZip.zipImage(file.getAbsolutePath());//压缩图片 mImageView.setImageURI(Uri.fromFile(file));//得到图片 } } }
布局
<Button android:id="@+id/button_start_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调用系统相机"/> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageview"/>
注意:在manifests中加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
压缩图片的方法
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; } }}
压缩图片中后面两个方法是Google的压缩图片,第一个是老师写的
查看相册中照片的代码
mButtonOpenGallery= (Button) findViewById(R.id.button_open_gallery); mButtonOpenGallery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(Intent.ACTION_GET_CONTENT);//启动存有相册的活动 intent.setType("image/*"); startActivityForResult(intent,GET_PIC_FORM_GALLERY); } }); // ********************************* protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK) { switch(requestCode){ case 0x23: ImageZip.zipImage(file.getAbsolutePath());//压缩图片 mImageView.setImageURI(Uri.fromFile(file));//得到图片 break; case GET_PIC_FORM_GALLERY: Uri uri=data.getData(); mImageView.setImageURI(uri); break; } } }
压缩相册里的原有图片的方法
public String getFilePathFromUri(Uri uri) { String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(uri, proj, null, null, null); int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(index); cursor.close(); cursor = null; return path; }
然后只需要在主函数里调用这个方法就行了
case GET_PIC_FORM_GALLERY: Uri uri = data.getData(); //根据Uri在数据库中查询出文件详细信息,从数据库中查出详细路径,然后在压缩 String s = getFilePathFromUri(uri); ImageZip.zipImage(s);//压缩图片-----ImageZip这个类已经在上面写了。 mImageView.setImageURI(uri); break;
0 0
- Android 多媒体应用:开启摄像头、查看相册中的照片
- Android 多媒体应用:从相册选择照片显示(解决相册中图片显示不出来的问题)
- UIImagePickerController--查看相册中的照片和视频
- Android 调用摄像头拍照 以及 从相册中选择照片
- Android 调用手机相册、摄像头拍照及剪裁照片
- Android从摄像头或相册中获取照片
- Android多媒体应用开发-控制摄像头拍照
- Android多媒体应用开发-控制摄像头拍照
- Android 多媒体应用:调用摄像头拍照
- 获取相册中的照片
- Android 多媒体应用——调用系统相机和相册
- Android多媒体之调用摄像头和从本地相册中选择图片
- Android学习笔记(八)使用多媒体(通知、短信、摄像头、相册、音视频)
- android调用摄像头拍照,从相册中选择照片并裁剪
- Android之调用摄像头拍照和从相册中选择照片
- android读取相册照片和相机照片
- 第十一章:Android中的多媒体应用
- 多媒体之——从相册选择照片
- Servlet技术基础
- OS之设备管理
- 关于jQuery双事件多重加载的问题。
- TCP通信的建立断开和状态转移
- Linux常用命令备忘录
- Android 多媒体应用:开启摄像头、查看相册中的照片
- 基本正则表达式
- 自动ping范围内的地址
- 【CSS】单行图片与文字垂直居中
- [Usaco2007 Open]City Horizon 城市地平线
- ASP.NET网站页面加载及运行效率等多方面实战优化
- 常用网址
- hashmap的实现原理分析
- RBAC权限模型