问题Camera.start preVIEW()问题、Camera、preVIEW、start

2023-09-06 18:57:24 作者:◆◇℡敷衍不嘚的爱丶

我在下面的本教程学习Android的摄像头API。我做了第一部分(权提供了一个覆盖开始之前)的结束,我得到了以下错误:

I'm following this tutorial to learn Android's Camera API. I made it the end of the first section (right before "Providing an overlay" begins), and I'm getting the following error:

06-20 23:33:50.903: ERROR/AndroidRuntime(1114):     at android.hardware.Camera.startPreview(Native Method)
06-20 23:33:50.903: ERROR/AndroidRuntime(1114):     at com.sobel.Sobel.startCamera(Sobel.java:73)
06-20 23:33:50.903: ERROR/AndroidRuntime(1114):     at com.sobel.Sobel.surfaceChanged(Sobel.java:36)

(完整跟踪)

Git的回购这里。主要活动这里。清单这里。

Git repo here. Main Activity here. Manifest here.

我检查并重新检查了我的code和已经按照教程一个T,那么这可能是造成这个错误?

I checked and re-checked my code and have followed the tutorial to a t, so what could be causing this error?

推荐答案

尝试设置表面的类型initCamera()。

Try setting the type of Surface in initCamera().

private void initCamera() {
mCamSV = (SurfaceView)findViewById(R.id.surface_camera);
mCamSH = mCamSV.getHolder();
mCamSH.addCallback(this);
**mCamSH.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);**

}

修改1

我复制它采用Android 2.2 SDK

I am copying all the files here which worked for me with android 2.2 sdk

活动

package com.stack.camera;


import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class CameraStackActivity extends Activity implements SurfaceHolder.Callback {
    private Camera mCam;
    private SurfaceView mCamSV;
    private SurfaceHolder mCamSH;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
    initCamera();
}

@Override
public void onDestroy() {
    stopCamera();
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    startCamera(holder, width, height);
}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mCam = Camera.open();
    try {
        mCam.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

private void initCamera() {
    mCamSV = (SurfaceView)findViewById(R.id.surface_camera);
    mCamSH = mCamSV.getHolder();
    mCamSH.addCallback(this);
    mCamSH.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}


private void startCamera(SurfaceHolder sh, int width, int height) {
    Camera.Parameters p = mCam.getParameters();
    // Camera.Size s = p.getSupportedPreviewSizes().get(0);
    p.setPreviewSize(width, height);

    mCam.setParameters(p);

    try {
        mCam.setPreviewDisplay(sh);
    } catch (Exception e) {
    }

    mCam.startPreview();
}

private void stopCamera() {
    mCamSH.removeCallback(this);

    mCam.stopPreview();
    mCam.release();
}

}

布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surface_camera"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.stack.camera"
      android:versionCode="1"
      android:versionName="1.0">

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="CameraStackActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

检查是否仍然不工作了你。

Check if it still doesnt work out for you.