Android相机预览闪烁

时间:2013-10-16 19:58:35

标签: android android-camera

我正在尝试编写一个简单的相机应用程序,但我的图片预览遇到了相当令人沮丧的问题。它显示在屏幕上运行的水平闪烁线。我的屏幕宽度为800(Galaxy SII plus),因此边缘有黑色空格(活动设置为水平方向)。现在,当闪烁发生时,这些黑色边缘开始转变为底端的白灰色,所以我怀疑它们可能会有一些东西。 真正讨厌的部分是,这似乎并不依赖于实际的代码,因为它有时可以在没有任何闪烁的情况下工作。

public class Preview extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mHolder;
    private Camera camera;
    private Point size;

Preview(Context context, Camera camera, Point size) {
    super(context);
    this.camera = camera;
    this.size = size;
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    setWillNotDraw(false);
    Parameters parameters = camera.getParameters();

    parameters.setPreviewSize(720, 480);
    parameters.setPictureSize(720, 480);

    camera.setParameters(parameters);
    Camera.Size previewSize = camera.getParameters().getPreviewSize();
    setLayoutParams(new FrameLayout.LayoutParams(previewSize.width,
            previewSize.height, Gravity.CENTER));

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    if (mHolder.getSurface() == null) {
        return;
    }

    // stop preview before making changes
    try {
        camera.stopPreview();
    } catch (Exception e) {
    }

    Camera.Size previewSize = camera.getParameters().getPreviewSize();
    setLayoutParams(new FrameLayout.LayoutParams(previewSize.width,
            previewSize.height, Gravity.CENTER));

    try {
        camera.setPreviewDisplay(mHolder);
        camera.startPreview();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {

}

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {

}

public void setCamera(Camera camera) {
    if (this.camera == camera) {
        return;
    }

    stopPreviewAndFreeCamera();

    this.camera = camera;

    if (this.camera != null) {
        requestLayout();

        try {
            this.camera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.camera.startPreview();
    }

}

public void stopPreviewAndFreeCamera() {

    if (this.camera != null) {
        this.camera.stopPreview();

        this.camera.release();

        this.camera = null;
    }
}

public void takePicture() {
    camera.autoFocus(null);
    camera.takePicture(null, null, new PictureHandler(this.getContext()));
    camera.startPreview();
}
}

public class MainActivity extends Activity {
private Preview mPreview;

    .
    .
    .

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        this.mPreview.stopPreviewAndFreeCamera();
        finish();
    }
    return super.onKeyDown(keyCode, event);
}
    }

0 个答案:

没有答案