简体   繁体   English

在surfaceview中预览Android相机

[英]Android camera preview in surfaceview

I manage to put camera preview in surfaceview and this works great. 我设法将相机预览放在surfaceview中,这很有效。 But now i have problem with speed of other components. 但现在我对其他组件的速度有问题。 Because now it is really slow. 因为现在它真的很慢。

Do i need to put camera in new thread? 我需要将相机放入新线程吗? How to decrease fps or resolution? 如何降低fps或分辨率? Because this what i have now does not work properly. 因为我现在所拥有的不能正常工作。

My surface view: 我的表面视图:

class KameraSurface extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "Preview";

    SurfaceHolder mHolder;
    public Camera camera;

    KameraSurface(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setFormat(PixelFormat.RGB_332);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.

         camera = Camera.open();

         Camera.Parameters p = camera.getParameters();
         p.setPictureSize(80, 60);
         p.setColorEffect(android.hardware.Camera.Parameters.EFFECT_NONE);
         p.setJpegQuality(20);
         p.setPreviewFrameRate(1);
         p.setPreviewFpsRange(5, 10);
         p.setPreviewSize(80, 60);
         camera.setParameters(p);



        try {
            camera.setPreviewDisplay(holder);


            camera.setPreviewCallback(new PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera arg1) {
                    //KameraSurface.this.invalidate();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        camera.stopPreview();
        camera = null;
    }
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(80, 60);
        camera.setParameters(parameters);

        camera.startPreview();
    }

    @Override
    public void draw(Canvas canvas) {
            super.draw(canvas);
            Paint p= new Paint(Color.RED);
            Log.d(TAG,"draw");
            canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p );
    }
}

My xml: 我的xml:

.
.
.
 <TableRow
            android:layout_width="match_parent"
            android:layout_height="fill_parent" >

            <FrameLayout
                android:id="@+id/preview"
                android:layout_width="133dp"
                android:layout_height="100dp"
                android:layout_margin="20dp" >
            </FrameLayout>
.
.
.

And how i call in main activity: 以及我如何调用主要活动:

KameraSurface preview = new KameraSurface(getApplicationContext());
                ((FrameLayout) findViewById(R.id.preview)).addView(preview);

You have a choice to start "startPreview" inside a different thread .. 您可以选择在不同的线程中启动“startPreview”。

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(80, 60);
    camera.setParameters(parameters);

    Thread preview_thread = new Thread(new Runnable() {
        @Override
        public void run() {
               camera.startPreview();
           }
        }, "preview_thread");
        preview_thread.start(); 
    }

I am facing same problem but I got this preview size by this method and now my camera preview working properly. 我面临同样的问题,但我通过这种方法得到了这个预览尺寸,现在我的相机预览工作正常。

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 0.1;
            double targetRatio = (double) h / w;

            if (sizes == null)
                return null;

            Camera.Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;

            int targetHeight = h;

            for (Camera.Size size : sizes) {
                double ratio = (double) size.height / size.width;
                if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                    continue;

                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }

            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Camera.Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }

            return optimalSize;
        }

    }

I have not tested your code but there are many factors that could contribute to a sluggish experience, including poor hardware. 我还没有测试过您的代码,但有许多因素可能会导致体验不佳,包括硬件不佳。 If you are using an emulator in Windows try setting the affinity of process/application to any CPU core other than 0. It runs faster in single core mode. 如果您在Windows中使用模拟器,请尝试将进程/应用程序的关联设置为0以外的任何CPU核心。它在单核模式下运行速度更快。

Do i need to put camera in new thread? 我需要将相机放入新线程吗?

No. As you can read here https://stackoverflow.com/a/1243448/649979 , it is already in its own thread separate to the the GUI thread. 不可以。你可以在这里阅读https://stackoverflow.com/a/1243448/649979 ,它已经在自己的线程中与GUI线程分开了。

However as we found out through the comments hardware acceleration helps <application android:hardwareAccelerated="true" ...> http://developer.android.com/guide/topics/graphics/hardware-accel.html 然而,正如我们通过评论发现硬件加速帮助<application android:hardwareAccelerated="true" ...> http://developer.android.com/guide/topics/graphics/hardware-accel.html

How to decrease fps or resolution? 如何降低fps或分辨率? Because this what i have now does not work properly. 因为我现在所拥有的不能正常工作。

How is supposed to work? 该怎么办? You have not outlined this. 你没有概述这一点。

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

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