在以编程方式加载android时,surfaceview闪烁黑色

时间:2014-12-23 13:20:03

标签: android camera

我正在使用相机预览在水面视图中加载相机。但是当它在表面添加一个视图时,它会闪烁一个黑色屏幕一秒钟。如何避免这一个。下面是我的活动和相机预览课程

      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.qr_code);
    //mCamera = getCameraInstance();
    qrCodeButton=(ImageButton) findViewById(R.id.qrcode_button);
    settingsButton=(ImageButton) findViewById(R.id.settings_button);
    mapLocationButton=(ImageButton) findViewById(R.id.location_button);
    backButton=(ImageButton) findViewById(R.id.back_button);
    iconButton=(ImageButton) findViewById(R.id.icon_button);

    preview = (FrameLayout)findViewById(R.id.cameraPreview);

   new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            autoFocusHandler = new Handler();
            releaseCamera();
            mCamera = getCameraInstance();
            scanner = new ImageScanner();
            scanner.setConfig(0, Config.X_DENSITY, 3);
            scanner.setConfig(0, Config.Y_DENSITY, 3);
            if(mCamera!=null)
            {
                mPreview = new CameraPreview(getApplicationContext(), mCamera, previewCb, autoFocusCB);
                // FrameLayout preview = (FrameLayout)rootView.findViewById(R.id.cameraPreview);
                preview.addView(mPreview);
                 v = new ImageView(getApplicationContext());
                // RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                 //v.setLayoutParams(layoutParams);
                v.setImageResource(R.drawable.qrscanbg2);
                preview.addView(v);

                //scannedResult(barcodeScanned);
                if (barcodeScanned) {
                    barcodeScanned = false;
                    //scanText.setText("Scanning...");
                    mCamera.setPreviewCallback(previewCb);
                    mCamera.startPreview();
                    previewing = true;
                    mCamera.autoFocus(autoFocusCB);
                }
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Failed to Initialise camera", Toast.LENGTH_SHORT).show();
            }
        }
    }, 1000);
}

这是我的相机预览课程

            public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private PreviewCallback previewCallback;
private AutoFocusCallback autoFocusCallback;
List<Size> mSupportedPreviewSizes;

public CameraPreview(Context context, Camera camera,
                     PreviewCallback previewCb,
                     AutoFocusCallback autoFocusCb) {
    super(context);
    mCamera = camera;
    previewCallback = previewCb;
    autoFocusCallback = autoFocusCb;

    /* 
     * Set camera to continuous focus if supported, otherwise use
     * software auto-focus. Only works for API level >=9.
     */
    /*
    Camera.Parameters parameters = camera.getParameters();
    for (String f : parameters.getSupportedFocusModes()) {
        if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
            mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            autoFocusCallback = null;
            break;
        }
    }
    */

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

    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}


public void setCamera(Camera camera) {
    mCamera = camera;
    if (mCamera != null) {
        mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
        requestLayout();
    }
}
public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        mCamera.setPreviewDisplay(holder);
    } catch (IOException e) {
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // Camera preview released in activity

}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    /*
     * If your preview can change or rotate, take care of those events here.
     * Make sure to stop the preview before resizing or reformatting it.
     */
    if (mHolder.getSurface() == null){
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e){
      // ignore: tried to stop a non-existent preview
    }

    try {
        // Hard code camera surface rotation 90 degs to match Activity view in portrait
        mCamera.setDisplayOrientation(90);
        mCamera.setPreviewDisplay(mHolder);
        mCamera.setPreviewCallback(previewCallback);
        mCamera.startPreview();
        mCamera.autoFocus(autoFocusCallback);
    } catch (Exception e){
      //  Log.d("DBG", "Error starting camera preview: " + e.getMessage());
    }
}

}

1 个答案:

答案 0 :(得分:0)

显然,将SurfaceView动态添加到没有SurfaceView刷新整个窗口的活动中。检查下面的答案。 https://stackoverflow.com/a/12636285/1705641