您好!欢迎来到源码码网

Android调用相机进行拍照

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-13 17:34
  • 阅读:546

Android开发中,借助于 Intent可以方便地调用 Android 系统的照相机程序进行拍照。但是需要声明摄像头的使用权限,即在 AndroidManifest.xml 文件中添加如下代码:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>

实例 CameraDemo 演示了通过 Intent 调用系统的拍照程序并返回照片的过程,该实例运行效果如图 1 所示。

CameraDemo实例运行效果

图 1  CameraDemo实例运行效果

当单击“启动摄像头”按钮时,启动 Android系统自带的照相机应用程序进行拍照,并将拍摄的照片显示到 ImageView 组件中。

实例 CameraDemo 中的 main.xml 代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">


  6. <Button

  7. android:id="@+id/button1"

  8. android:layout_width="wrap_content"

  9. android:layout_height="wrap_content"

  10. android:text="@string/camera" />


  11. <ImageView

  12. android:id="@+id/imageview"

  13. android:layout_width="wrap_content"

  14. android:layout_height="wrap_content" />

  15. </LinearLayout>

在实例 CameraDemo 中的 AndroidManifest.xml 代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="introduction.android.receivemessagedemo"

  4. android:versionCode="1"

  5. android:versionName="1.0">


  6. <uses-sdk android:minSdkVersion="14" />


  7. <uses-permission android:name="android.permission.CAMERA" />

  8. <uses-feature android:name="android.hardware.camera" />

  9. <application

  10. android:allowBackup="true"

  11. android:icon="@mipmap/ic_launcher"

  12. android:label="@string/app_name"

  13. android:roundIcon="@mipmap/ic_launcher_round"

  14. android:supportsRtl="true"

  15. android:theme="@style/AppTheme">

  16. <activity android:name=".MainActivity">

  17. <intent-filter>

  18. <action android:name="android.intent.action.MAIN" />

  19. <category android:name="android.intent.category.LAUNCHER" />

  20. </intent-filter>

  21. </activity>

  22. </application>


  23. </manifest>

在实例 CameraDemo 中的 CameraDemoActivity.java 代码如下:


  1. import android.app.Activity;

  2. import android.content.Intent;

  3. import android.graphics.Bitmap;

  4. import android.os.Bundle;

  5. import android.provider.MediaStore;

  6. import android.util.Log;

  7. import android.view.View;

  8. import android.view.View.OnClickListener;

  9. import android.widget.Button;

  10. import android.widget.ImageView;


  11. public class MainActivity extends Activity {

  12. /**

  13.     * Called when the activity is first created.

  14.     */

  15. private ImageView imageview;

  16. private Button btn;


  17. @Override

  18. public void onCreate(Bundle savedInstanceState) {

  19. super.onCreate(savedInstanceState);

  20. setContentView(R.layout.activity_main);

  21. imageview = (ImageView) findViewById(R.id.imageview);

  22. btn = (Button) findViewById(R.id.button1);

  23. btn.setOnClickListener(new OnClickListener() {

  24. public void onClick(View v) {

  25. // TODO Auto-generated method stub

  26. try {

  27. Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

  28. startActivityForResult(i, 1);

  29. } catch (Exception e) {

  30. Log.d("cameraDemo", e.toString());

  31. }

  32. }

  33. });

  34. }


  35. protected void onActivityResult(int requestcode, int resultCode, Intent data) {

  36. try {

  37. if (requestcode != 1) {

  38. return;

  39. }

  40. super.onActivityResult(requestcode, resultCode, data);

  41. Bundle extras = data.getExtras();

  42. Bitmap bitmap = (Bitmap) extras.get("data");

  43. imageview.setImageBitmap(bitmap);

  44. } catch (Exception e) {

  45. Log.d("cameraDemo", e.toString());

  46. }

  47. }

  48. }

在启动摄像头程序时,因为要传回拍摄的图像,所以调用了 Activity.startActivityForResult(Intent intent, int requestCode) 方法。

当 startActivityForResult() 方法启动的 Activity 正常结束时,会自动返回发出请求的 Activity,并且该方法会返回对应的 requestCode 值给 onActivityResult(int requestcode, int resultCode,Intent data) 方法,借此可以在请求 Activity 和发出请求的 Activity 之间进行数据传递。本实例借助于这一特点传回了 Android 系统照相机程序拍摄的照片。

特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • bootstrap ui框架能用在uniapp中吗?
  • bootstrap ui框架能用在uniapp中吗?
  • BootstrapUI框架通常是前端开发中的一种工具,它提供了一套预定义的CSS样式和组件,用于快速构建响应式布局的网页。然而,UniApp是一个使用Vue.js开发跨平台应用的框架,它可以用来开发iOS、Android、以及各种小程序和H5应用。
  • 互动社区
  • 来源:源码码网
  • 编辑:热度建站
  • 时间:2024-04-12 00:04
  • 阅读:188
  • css实现banner图由中心点动态放大效果
  • css实现banner图由中心点动态放大效果
  • 在日常的网页设计中,为了让网页增加一定的特效以达到交互的目的,我们尝尝会在网页中使用一些动画效果。今天来说说实现banner图由中心点动态放大效果,实现这个效果需要用到css中的动画:animation​和关键帧:@keyframes,具体示例如下:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-11 18:52
  • 阅读:202
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • 在网页开发中为了提升网页的交互效果,经常会用到使用js给网页增加一定的特效,下边就来说说使用js实现鼠标滑动到某个位置数字自动滚动增加的效果。其实这种效果有很多中解决办法,自己也可以去写,下边我们借助countUp.js来实现,关于这个js文件,我放在末尾:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-08 09:20
  • 阅读:277
  • 响应式网页设计思路及注意事项
  • 响应式网页设计思路及注意事项
  • 一、什么是响应式网页响应式网页设计就是让网页具有根据设备类型应用CSS样式的能力。设计:设想、计划。设计就是把想法实现。网页设计:按照一定的设计思路布局网页内容。传统网页设计:都是针对PC端浏览器而设计的,不具备查询设备的能力,更不能对多种访问设备做出响应。传统网页设计的弊端:在移动互联网时代,传统的网页设计不适合多屏幕时代。响应式网页设计应运而生。响应式网页设计是一种设计网页的思想/方法。响应:指让我们的网页能够自动查询用户的访问设备
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-02 11:24
  • 阅读:184
  • css中rel的属性值都有哪些,分别代表什么意思
  • css中rel的属性值都有哪些,分别代表什么意思
  • 在HTML中,元素的rel属性用于定义当前文档与被链接文档之间的关系。这个属性在CSS的上下文中经常与样式表关联,但rel属性的用途远不止于此。以下是一些常见的rel属性值及其意义:1、stylesheet:表示被链接的文档是一个样式表。这通常用于链接CSS文件。
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-03-28 12:28
  • 阅读:314
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部