如何显示我的相机预览 [英] How can I show my camera preview

查看:37
本文介绍了如何显示我的相机预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个相机预览类

public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
    private Camera camera;

    public CameraView(Context context) {
        super(context);
        getHolder().addCallback(this);
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }


    public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera){
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, info);
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation){
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;
        }else{
            result = (info.orientation - degrees + 360) % 360;
        }

        camera.setDisplayOrientation(result);

    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        camera = Camera.open();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        setCameraDisplayOrientation((Activity)getContext(), Camera.CameraInfo.CAMERA_FACING_BACK, camera);
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size bestSize = null;
        for (Camera.Size size : parameters.getSupportedPictureSizes()){
            if (size.width <= i1 && size.height <=i2) {
                if (bestSize == null) {
                    bestSize = size;
                } else {
                    int bestArea = bestSize.width * bestSize.height;
                    int newArea = size.width * size.height;

                    if (newArea > bestArea){
                        bestSize = size;
                    }
                }
            }
        }

        parameters.setPictureSize(bestSize.width, bestSize.height);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        camera.setParameters(parameters);

        try{
            camera.setPreviewDisplay(surfaceHolder);
        }catch (IOException e){
            e.printStackTrace();
        }

        camera.startPreview();


    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }


}

然后我写了一个布局文件,我想在SurfaceView"中显示预览

And then I wrote a layout file and I want to display the preview in the "SurfaceView"

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

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/camera_view" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:weightSum="1">

        <Button
            android:id="@+id/stop_navigation"
            android:layout_weight="0.5"
            android:layout_height="32dp"
            android:layout_width="0dp"
            android:text="@string/stop_navigation"
            android:textColor="@color/colorWhite"
            android:textSize="12sp"
            android:background="@color/colorAccent" />
    </LinearLayout>

</RelativeLayout>

那么我在主要活动中应该怎么做呢?在我执行以下代码之前,一切都很好.

So how should I do in the main activity? Before I just did the below code and everything is fine.

CameraView cameraView = new CameraView(this);
addContentView(cameraView, new ActionBar.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT));

但现在我需要将预览放入我设计的布局中.

But now I need to put the preview into the layout I design.

推荐答案

试试这个:

您需要在 XML 文件中更改您的 SurfaceView.

You need to Change in your SurfaceView in XML File.

用这个替换你的surfaceView

Replace your surfaceView with this

<CameraView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/camera_view" />

你剩下的代码是对的.更改后通知我

and your remaining code is Right.let me know after change

希望这会有所帮助...(:

Hope this will helps...(:

这篇关于如何显示我的相机预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆