在surfaceview中预览Android相机

时间:2012-08-07 12:02:06

标签: android camera surfaceview

我设法将相机预览放在surfaceview中,这很有效。但现在我对其他组件的速度有问题。因为现在它真的很慢。

我需要将相机放入新线程吗?如何降低fps或分辨率?因为我现在所拥有的不能正常工作。

我的表面视图:

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

我的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>
.
.
.

我如何在主要活动中打电话:

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

3 个答案:

答案 0 :(得分:2)

您可以选择在不同的线程中启动“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(); 
    }

答案 1 :(得分:2)

我遇到同样的问题,但我通过这种方法得到了这个预览尺寸,现在我的相机预览正常工作。

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

    }

答案 2 :(得分:0)

我还没有测试过您的代码,但有很多因素可能导致体验不佳,包括硬件不佳。如果您在Windows中使用模拟器,请尝试将进程/应用程序的关联设置为0以外的任何CPU核心。它在单核模式下运行速度更快。

  

我需要将相机放入新线程吗?

没有。你可以在这里阅读https://stackoverflow.com/a/1243448/649979,它已经在它自己的线程中与GUI线程分开了。

但是,正如我们通过评论发现的那样,硬件加速有助于<application android:hardwareAccelerated="true" ...> http://developer.android.com/guide/topics/graphics/hardware-accel.html

  

如何降低fps或分辨率?因为这就是我现在所拥有的   不能正常工作。

该怎么办?你没有概述这个。