阿尔卡特OneTouch KitKat surfaceView与相机爆发无限循环 - SurfaceView(8099):updateWindow - OnPreDrawListener,mHaveFrame = true

时间:2014-11-11 16:04:53

标签: android

以下代码在我使用Jelly Bean的LG G Pro上完美运行 但是在使用KitKat 4.4.2的Alcatel OneTouch上无法正常工作

编辑:最糟糕的是,这适用于搭载4.4.2的HTC,但不适用于阿尔卡特。 WTF。

这是我的自定义相机代码:

    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.e("camera fragment", "surfaceCreated");

    int width = getResources().getDisplayMetrics().widthPixels;
    int height = getResources().getDisplayMetrics().heightPixels;

    // background
    Bitmap background = null;
    try {
        background = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    } catch (OutOfMemoryError oome) {
        oome.printStackTrace();
        try {
            background = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        } catch (OutOfMemoryError oome2) {
            oome2.printStackTrace();
        }

    }

    Canvas canvas = null;
    if (background != null) {
        canvas = new Canvas(background);
        canvas.drawColor(Color.TRANSPARENT);
        // draw lines
        // vertical
        drawVerticalLine(canvas);
        // horizontal
        drawHorizontalLine(canvas);
        // draw text
        drawText(canvas);
    }

    BitmapDrawable d = new BitmapDrawable(getResources(), background);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        surfaceView.setBackgroundDrawable(d);
    } else {
        surfaceView.setBackground(d);
    }

    if (camera == null)
        initCamera();

    startCamera(holder);


}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Log.e("camera fragment", "surfaceChanged");

    Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    Camera.getCameraInfo(0, info);
    int rotation = a.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    if (camera != null) {
        camera.setDisplayOrientation(result);
        camera.startPreview();
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // Surface will be destroyed when we return, so stop the preview.
    stopCamera();
    Log.e("Camera Fragment", "surfaceDestroyed");
}

private void initCamera() {
    Log.e("initCamera", "initCamera");
    try {
        camera = Camera.open();
    } catch (RuntimeException e) {
        e.printStackTrace();
    }

}

private void startCamera(SurfaceHolder holder) {

    try {
        camera.setPreviewDisplay(holder);
        camera.autoFocus(new AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                // TODO Auto-generated method stub

            }
        });
    } catch (Exception exception) {
        if (camera != null) {
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    }

}

private void stopCamera() {

    if (camera != null) {
        camera.stopPreview();
        camera.release();
        surfaceHolder.removeCallback(this);
        camera = null;
    }

}

当我启动包含此内容的片段时,以下是日志显示的内容:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

问题出现在以下代码中:

    camera.autoFocus(new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            // TODO Auto-generated method stub

        }
    });

在使用此代码之前,我应该根据文档检查设备是否支持自动对焦:

Callers should check android.hardware.Camera.Parameters.getFocusMode() to determine if
 this method should be called. If the camera does not support auto-focus, 
it is a no-op and AutoFocusCallback.onAutoFocus(boolean, Camera)
 callback will be called immediately.