3

I manage to put camera preview in surfaceview and this works great. 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? 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:

.
.
.
 <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);
4

3 回答 3

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(); 
    }
于 2012-08-07T12:12:29.060 回答
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;
        }

    }
于 2016-05-18T11:31:13.240 回答
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或分辨率?因为这是我现在所拥有的不能正常工作。

应该如何工作?你没有概述这一点。

于 2012-08-07T12:11:16.577 回答