应用程序中的基本相机

时间:2016-07-06 09:59:17

标签: android

我只是想在我的应用程序中激活相机我已经完成了以下操作,并尽可能简单地保持它。

AndroidManifest文件

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
...
    <activity
        android:name=".CameraAct"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

CameraAct.class

   public class CameraAct extends AppCompatActivity {

       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_camera);

        // do we have a camera?
        if (!getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
                    .show();
        } else {
            cameraId = findFrontFacingCamera();
            if (cameraId < 0) {
                Toast.makeText(this, "No front facing camera found.",
                        Toast.LENGTH_LONG).show();
            } else {
                camera = Camera.open(cameraId);
            }
          }
       }

       private int findFrontFacingCamera() {
          int cameraId = -1;
          // Search for the front facing camera
          int numberOfCameras = Camera.getNumberOfCameras();
          for (int i = 0; i < numberOfCameras; i++) {
             CameraInfo info = new CameraInfo();
             Camera.getCameraInfo(i, info);
             if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                Log.d(DEBUG_TAG, "Camera found");
                cameraId = i;
                break;
            }
          }
        return cameraId;
    }

activity_camera.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

</RelativeLayout>

活动打开,在日志中我看到debug_tag:在logcat中找到相机,但是我在屏幕上看不到相机。这是我在活动启动时看到的内容: enter image description here

0 个答案:

没有答案