拍照并获取图片
2015-11-27 09:07
183 查看
用户权限
代码示例:
1.开启相机
2.获取图片
文件转换为bitmap
<uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />
代码示例:
1.开启相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/test.jpg"))); startActivityForResult(intent, 1);
2.获取图片
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (resultCode == Activity.RESULT_OK) { Bitmap cameraBitmap = decodeBitmp("/sdcard/test.jpg"); imageView.setImageBitmap(cameraBitmap); // 存成320*240和50*50 如果是垂直方向,需要对cameraBitmap旋转90度 //MediaStore.Images.Media.insertImage(getContentResolver(), cameraBitmap, null, null); } } super.onActivityResult(requestCode, resultCode, data); }
文件转换为bitmap
public static final float DISPLAY_WIDTH = 200; public static final float DISPLAY_HEIGHT = 200; /** * 从path中获取图片信息 * @param path * @return */ private Bitmap decodeBitmap(String path){ BitmapFactory.Options op = new BitmapFactory.Options(); //inJustDecodeBounds //If set to true, the decoder will return null (no bitmap), but the out… op.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息 //获取比例大小 int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH); int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT); //如果超出指定大小,则缩小相应的比例 if(wRatio > 1 && hRatio > 1){ if(wRatio > hRatio){ op.inSampleSize = wRatio; }else{ op.inSampleSize = hRatio; } } op.inJustDecodeBounds = false; bmp = BitmapFactory.decodeFile(path, op); return bmp; }
相关文章推荐
- 第八课 魔棒 画笔工具和铅笔工具
- Tomcat设置虚拟目录的方法, 不修改server.xm
- MINIST data set input_data.py
- 第七课 渐变工具和油漆桶工具
- 第六课 移动工具
- Tomcat虚拟根目录与虚拟目录
- Javascript - 定义函数直接调用
- 第十三周--项目5拓扑排序算法验证
- 微信接口尝试
- 图片浏览器(一)
- 第五课 套索工具
- 使用wfastcgi在IIS上部署Python Flask应用
- Linux清除原有ssh密钥方法
- 第13周项目5-拓扑排序算法的验证
- 第十一周-用二叉树解代数表达式
- ccna-daytwo
- 第十二周--数据结构--求出度最大的一个顶点并输出该顶点编号
- mysql 建数据库、建表、插入数据、增删列
- 临时表与正式表的区别
- 第四课 羽化与抓手