5

我试图编写一个简单的相机应用程序,但我的图片预览遇到了一个非常令人沮丧的问题。它显示在屏幕上运行的水平闪烁线。我的屏幕宽度为 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);
}
    }
4

0 回答 0