Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
2023-09-14 08:59:39 时间
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" Button android:id="@+id/on_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开相机" / /RelativeLayout
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); on_camera = (Button) findViewById(R.id.on_camera); on_camera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { * 我们使用Intent的方式打开系统相机 * 1.直接跳转相机包名,前提是你知道这个应用的包名 * 2.就是使用隐式Intent了,在这里我们就使用隐式intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //指定拍照 startActivity(intent);
说到获取图片,大家应该就立马想到了startActivityForResult,没错,我们就是这样直接拿到传递回来的data,然后通过Bundle转换二进制流的方式获得一个bitmap,好的,这样的话,我们就要新增加一个imageview了
LinearLayout 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" android:orientation="vertical" Button android:id="@+id/on_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开相机" / ImageView android:id="@+id/iv_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" / /LinearLayout
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_photo = (ImageView) findViewById(R.id.iv_photo); on_camera = (Button) findViewById(R.id.on_camera); on_camera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { * 我们使用Intent的方式打开系统相机 * 1.直接跳转相机包名,前提是你知道这个应用的包名 * 2.就是使用隐式Intent了,在这里我们就使用隐式intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 指定拍照\ // 拍照返回图片 startActivityForResult(intent, CODE); @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 == CODE) { // 获取图片 Bundle bundle = data.getExtras(); // 转换图片的二进制流 Bitmap bitmap = (Bitmap) bundle.get("data"); // 设置图片 iv_photo.setImageBitmap(bitmap);
这里我们要知道一个概念,就是我们返回结果是从data中取出来的,但是这个data不可能存储太多的东西,比如你拍的4K照片,动辄几十M,那不就直接挂了嘛,google是这样设定的,data返回的只是有一个缩略图,但是我们实际开发当中怎么可以使用缩略图,别急,是有办法的
我们再增加一个button和一个imageview为了显示真实大小的图片,这里就直接上完整代码了,思路十分的顺,但是我们所用到的方式是要写sd卡的,我们新增加一个权限
LinearLayout 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" android:orientation="vertical" Button android:id="@+id/on_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开相机" / Button android:id="@+id/on_camera_big" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大图片" / ImageView android:id="@+id/iv_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" /
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取SD卡路径 mFilePath = Environment.getExternalStorageDirectory().getPath(); // 文件名 mFilePath = mFilePath + "/" + "photo.png"; iv_photo = (ImageView) findViewById(R.id.iv_photo); iv_photo_big = (ImageView) findViewById(R.id.iv_photo_big); on_camera = (Button) findViewById(R.id.on_camera); on_camera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { * 我们使用Intent的方式打开系统相机 * 1.直接跳转相机包名,前提是你知道这个应用的包名 * 2.就是使用隐式Intent了,在这里我们就使用隐式intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 指定拍照 // 拍照返回图片 startActivityForResult(intent, CODE); on_camera_big = (Button) findViewById(R.id.on_camera_big); on_camera_big.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 指定拍照 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 加载路径 Uri uri = Uri.fromFile(new File(mFilePath)); // 指定存储路径,这样就可以保存原图了 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // 拍照返回图片 startActivityForResult(intent, CODEBIG); @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 == CODE) { // 获取图片 Bundle bundle = data.getExtras(); // 转换图片的二进制流 Bitmap bitmap = (Bitmap) bundle.get("data"); // 设置图片 iv_photo.setImageBitmap(bitmap); // 加载原图 } else if (requestCode == CODEBIG) { try { // 获取输入流 is = new FileInputStream(mFilePath); // 把流解析成bitmap Bitmap bitmap = BitmapFactory.decodeStream(is); // 设置图片 iv_photo_big.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭流 try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
“千变万化”——神奇的Android图片规格调整器(功能梳理篇) 由于放假的缘故,还是没时间直接上手APP,所以趁着晚上有时间,不妨为“千变万化”APP梳理一下功能。这样也能为我构思该APP提供更好的设计方向,防止出现想到一处写一处的混乱情况。
相关文章
- Android开发之布局文件中实现OnClick事件关联处理方法
- Android开发之Gson解析Json嵌套数据
- 如何将为Android开发的AIR应用转移到SD卡
- [Android Pro] Android权限设置android.permission完整列表
- 使用IntelliJ IDEA 13搭建Android集成开发环境(图文教程)
- Android开发之旅:android架构
- Android 开发之旅:短信的收发及在android模拟器之间实践(二)
- android开发学习---linux下开发环境的搭建&& android基础知识介绍
- 【ANDROID游戏开发十六】ANDROID GESTURE之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- 【COCOS2DX-LUA 脚本开发之十四】解决自定义CPP类通过TOLUA++ BINDING LUACOCOS2D后编译到ANDROID运行黑屏(没有调用自定义CPP类)的问题!
- Android开发艺术探索——新的征程,程序人生路漫漫!
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
- 《android开发艺术探索》读书笔记(十)--Android的消息机制
- 《android开发艺术探索》读书笔记(四)--View工作原理
- android开发分享Android中外接键盘的检测的实现
- ADT(Android)—Eclipse开发AndroidManifest.xml在哪里
- ADT(Android) — Eclipse开发连接夜神模拟器方法(亲测有效!)
- Android 10.0 SystemUI下拉状态栏UI定制化开发系列(二)
- Android音视频开发(一):通过三种方式绘制图片
- 【Android 安全】DEX 加密 ( Java 工具开发 | apk 文件签名 )
- 【Android 安全】DEX 加密 ( 代理 Application 开发 | multiple-dex-core 依赖库开发 | 配置元数据 | 获取 apk 文件并准备相关目录 )
- android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
- android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
- Android - 控件android:ems属性
- Android 开发 --Unable to resolve target 'android-19'
- Android开发 从多个Activity返回到第一个Activity时如何实现“再按一次退出程序”
- android开发,Android Studio Build Output 输出的中文显示乱码