Android 多媒体应用:调用摄像头拍照
2015-09-16 09:08
477 查看
在实际的应用程序里我们可能会使用到摄像头比如我们平时使用的淘宝,评价时可以拍照上传,还有平时使用的美颜相机。我们在自己的应用程序中也可以实现调用摄像头的功能。
2、MainActivity
3、最后不要忘记添加读写Sdcard的权限
一、调用摄像头实现拍照功能
启动摄像头其实很简单,步骤如下。
[code]Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); startActivity(intent);
示例
1、布局[code]<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=".MainActivity" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_start_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调用系统摄像头" /> </RelativeLayout>
2、MainActivity
[code] public class MainActivity extends Activity { private Button mbtn_start_camera; private ImageView mimageview; private File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mbtn_start_camera=(Button) findViewById(R.id.btn_start_camera); mimageview=(ImageView) findViewById(R.id.imageview); mbtn_start_camera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //创建Intent设置调用拍照功能 Intent intent=new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); //指定图片的输出地址 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); //这里调用startActivityForResult是为了响应以后的操作 startActivityForResult(intent, 0x23); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==0x23){ if(resultCode==RESULT_OK){ //进行压缩 zipImage(file.getAbsolutePath()); //将拍摄的照片直接显示在界面上面 mimageview.setImageURI(Uri.fromFile(file)); } } } //下面的方法是google发布的图片压缩的方法 //有的手机对显示的图片不进行限制,有的手机可能会有限制, //这时需要对图片进行压缩 private 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 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 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; } } }
3、最后不要忘记添加读写Sdcard的权限
[code] <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
相关文章推荐
- android设置时间日期
- Android开发学习笔记:Intent的简介以及属性的详解
- android批量文件上传(服务器采用servlet处理)
- Android中.9图片的了解和制作过程
- android资源目录
- 使用asm.jar将Android手机屏幕投影到电脑
- 0915Android基础摄像头
- Android 多媒体应用——MediaPlayer与SurfaceView播放视频
- AndroidStudio导入库文件
- Android 多媒体应用——VideoView播放视频
- 普渡大学开发了可延长 Android 电池 16% 的寿命的开源软件
- Android 动画效果(一): 四种动画基础(Alpha、Translate、Rotate、Scale)
- 将Android源代码导入SourceInsight
- Android开发环境的发展演变
- 调研Android的开发环境的发展演变
- Android中Intent对象与Intent Filter过滤匹配过程详解
- Activity 通过 Fragment保存大块数据的一种方法。摘自android开发官网 3ff8
- android 状态栏一体化实现
- 在版本低于android21下使用material progressbar风格的进度条
- Android 线性布局分析一