Android 相机预览有问题

Having problems on Android camera preview

我试图在 Activity 上设置 Android 相机预览但失败了。我的 Surface View 上只有黑屏。对其他人来说似乎太简单了,但我仍然无法弄清楚。 这是我的代码:

package com.example.peterchen.camerapreviewexample;

import android.hardware.Camera;
import android.graphics.PixelFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
Camera myCamera;
SurfaceView previewSurfaceView;
SurfaceHolder previewSurfaceHolder;

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

        previewSurfaceView = (SurfaceView)findViewById(R.id.surfaceView);
        previewSurfaceHolder = previewSurfaceView.getHolder();
        previewSurfaceHolder.addCallback(this);
        previewSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override
public void onResume() {
    super.onResume();
    myCamera.open();
    myCamera.startPreview();
}

@Override
public void onPause() {
    myCamera.stopPreview();
    myCamera.release();
    myCamera=null;

    super.onPause();
}


@Override
public void surfaceCreated(SurfaceHolder holder) {}

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

@Override
public void surfaceDestroyed(SurfaceHolder holder) {}

}

我的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.peterchen.camerapreviewexample.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView" />

<SurfaceView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/surfaceView"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

和我的清单 xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.peterchen.camerapreviewexample">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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>

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

非常感谢你们所有人。

你可以试试下面的代码

main.xml

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <SurfaceView
            android:id="@+id/surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1" >

        <Button
            android:id="@+id/startcamerapreview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Show Preview" />

        <Button
            android:id="@+id/stopcamerapreview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Stop Preview" />
    </LinearLayout>

</LinearLayout>

AndroidCamera.java

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidCamera extends Activity implements SurfaceHolder.Callback
    {

        Camera camera;
        SurfaceView surfaceView;
        SurfaceHolder surfaceHolder;
        Button buttonStartCameraPreview, buttonStopCameraPreview;
        boolean previewing = false;

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

                buttonStartCameraPreview = (Button) findViewById(R.id.startcamerapreview);
                buttonStopCameraPreview = (Button) findViewById(R.id.stopcamerapreview);

                surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
                surfaceHolder = surfaceView.getHolder();
                surfaceHolder.addCallback(this);
                surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

                buttonStartCameraPreview.setOnClickListener(new OnClickListener()
                    {

                        @Override
                        public void onClick(View v)
                            {
                                // TODO Auto-generated method stub
                                if (!previewing)
                                    {
                                        camera = Camera.open();
                                        if (camera != null)
                                            {
                                                try
                                                    {
                                                        camera.setPreviewDisplay(surfaceHolder);
                                                        camera.startPreview();
                                                        previewing = true;
                                                    } catch (IOException e)
                                                    {
                                                        // TODO Auto-generated catch block
                                                        e.printStackTrace();
                                                    }
                                            }
                                    }

                            }
                    });
                buttonStopCameraPreview.setOnClickListener(new OnClickListener()
                    {

                        @Override
                        public void onClick(View v)
                            {
                                // TODO Auto-generated method stub
                                if (camera != null && previewing)
                                    {
                                        camera.stopPreview();
                                        camera.release();
                                        camera = null;
                                        previewing = false;
                                    }

                            }
                    });
            }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
            {
                // TODO Auto-generated method stub
            }

        @Override
        public void surfaceCreated(SurfaceHolder holder)
            {
                // TODO Auto-generated method stub
            }

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

AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="8" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidCamera"
            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>

替换"AndroidCamera.java"你的90度屏幕旋转问题解决

  import android.app.Activity;
    import android.hardware.Camera;
    import android.os.Bundle;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class AndroidCamera extends Activity implements SurfaceHolder.Callback
        {

            Camera camera;
            SurfaceView surfaceView;
            SurfaceHolder surfaceHolder;
            Button buttonStartCameraPreview, buttonStopCameraPreview;
            boolean previewing = false;

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

                    buttonStartCameraPreview = (Button) findViewById(R.id.startcamerapreview);
                    buttonStopCameraPreview = (Button) findViewById(R.id.stopcamerapreview);

                    surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
                    surfaceHolder = surfaceView.getHolder();
                    surfaceHolder.addCallback(this);
                    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

                    buttonStartCameraPreview.setOnClickListener(new OnClickListener()
                        {

                            @Override
                            public void onClick(View v)
                                {
                                    // TODO Auto-generated method stub
                                    if (!previewing)
                                        {
                                            camera = Camera.open();
                                            if (camera != null)
                                                {
                                                    try
                                                        {
                                                            camera.setDisplayOrientation(90); 
                                                            camera.setPreviewDisplay(surfaceHolder);
                                                            camera.startPreview();
                                                            previewing = true;
                                                        } catch (IOException e)
                                                        {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                        }
                                                }
                                        }

                                }
                        });
                    buttonStopCameraPreview.setOnClickListener(new OnClickListener()
                        {

                            @Override
                            public void onClick(View v)
                                {
                                    // TODO Auto-generated method stub
                                    if (camera != null && previewing)
                                        {
                                            camera.stopPreview();
                                            camera.release();
                                            camera = null;
                                            previewing = false;
                                        }

                                }
                        });
                }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
                {
                    // TODO Auto-generated method stub
                }

            @Override
            public void surfaceCreated(SurfaceHolder holder)
                {
                    // TODO Auto-generated method stub
                }

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