如何在 Android 应用程序中使用相机?


此示例演示了如何在 Android 应用程序中使用相机。

步骤 1 - 在 Android Studio 中创建一个新项目,选择文件 ⇒ 新项目并填写所有必需的详细信息以创建一个新项目。

步骤 2 - 将以下代码添加到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:text="Take a Photo" >
   </Button>
   <ImageView
      android:id="@+id/imageView1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_above="@+id/button1"
      android:layout_alignParentTop="true"
      >
   </ImageView>
</RelativeLayout>

步骤 3 - 将以下代码添加到 src/MainActivity.java

package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
   private static final int CAMERA_REQUEST=1888;
   ImageView imageView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      imageView=(ImageView) this.findViewById(R.id.imageView1);
      Button photoButton=(Button) this.findViewById(R.id.button1);
      photoButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
         }
      });
   }
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == CAMERA_REQUEST) {
         Bitmap photo=(Bitmap) data.getExtras().get("data");
         imageView.setImageBitmap(photo);
      }
   }
}

步骤 4 - 将以下代码添加到 Manifests/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

让我们尝试运行您的应用程序。我假设您已将您的实际 Android 移动设备与您的计算机连接起来。要从 Android Studio 运行应用程序,请打开您项目的活动文件之一,然后单击工具栏中的运行Play 图标 图标。选择您的移动设备作为选项,然后查看将显示默认屏幕的移动设备 -

单击此处下载项目代码

更新于:2020 年 7 月 3 日

344 次浏览

开启你的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.