简体   繁体   English

Camera.startPreview()问题

[英]Problem with Camera.startPreview()

I'm following this tutorial to learn Android's Camera API. 我正在按照本教程学习Android的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)

( Full trace ) 全迹

Git repo here . Git仓库在这里 Main Activity here . 这里的主要活动。 Manifest here . 在这里显示

I checked and re-checked my code and have followed the tutorial to at, so what could be causing this error? 我检查并重新检查了我的代码,并按照本教程进行操作,因此可能导致此错误的原因是什么?

Try setting the type of Surface in initCamera(). 尝试在initCamera()中设置Surface的类型。

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

}

EDIT 1 编辑1

I am copying all the files here which worked for me with android 2.2 sdk 我正在复制所有适用于android 2.2 sdk的文件

Activity 活动

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();
}

} }

Layout 布局

<?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>

Manifest File 清单文件

<?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. 检查它是否仍然不适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM