Java Code Examples for android.hardware.Camera#autoFocus()

The following examples show how to use android.hardware.Camera#autoFocus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodeScanner.java    From code-scanner with MIT License 6 votes vote down vote up
private void safeAutoFocusCamera() {
    if (!mInitialized || !mPreviewActive) {
        return;
    }
    final DecoderWrapper decoderWrapper = mDecoderWrapper;
    if (decoderWrapper == null || !decoderWrapper.isAutoFocusSupported() ||
            !mAutoFocusEnabled) {
        return;
    }
    if (mSafeAutoFocusing && mSafeAutoFocusAttemptsCount < SAFE_AUTO_FOCUS_ATTEMPTS_THRESHOLD) {
        mSafeAutoFocusAttemptsCount++;
    } else {
        try {
            final Camera camera = decoderWrapper.getCamera();
            camera.cancelAutoFocus();
            camera.autoFocus(mSafeAutoFocusCallback);
            mSafeAutoFocusAttemptsCount = 0;
            mSafeAutoFocusing = true;
        } catch (final Exception e) {
            mSafeAutoFocusing = false;
        }
    }
    scheduleSafeAutoFocusTask();
}
 
Example 2
Source File: CodeScanner.java    From code-scanner with MIT License 5 votes vote down vote up
@SuppressWarnings("SuspiciousNameCombination")
void performTouchFocus(final Rect viewFocusArea) {
    synchronized (mInitializeLock) {
        if (mInitialized && mPreviewActive && !mTouchFocusing) {
            try {
                setAutoFocusEnabled(false);
                final DecoderWrapper decoderWrapper = mDecoderWrapper;
                if (mPreviewActive && decoderWrapper != null &&
                        decoderWrapper.isAutoFocusSupported()) {
                    final Point imageSize = decoderWrapper.getImageSize();
                    int imageWidth = imageSize.getX();
                    int imageHeight = imageSize.getY();
                    final int orientation = decoderWrapper.getDisplayOrientation();
                    if (orientation == 90 || orientation == 270) {
                        final int width = imageWidth;
                        imageWidth = imageHeight;
                        imageHeight = width;
                    }
                    final Rect imageArea =
                            Utils.getImageFrameRect(imageWidth, imageHeight, viewFocusArea,
                                    decoderWrapper.getPreviewSize(),
                                    decoderWrapper.getViewSize());
                    final Camera camera = decoderWrapper.getCamera();
                    camera.cancelAutoFocus();
                    final Parameters parameters = camera.getParameters();
                    Utils.configureFocusArea(parameters, imageArea, imageWidth, imageHeight,
                            orientation);
                    Utils.configureFocusModeForTouch(parameters);
                    camera.setParameters(parameters);
                    camera.autoFocus(mTouchFocusCallback);
                    mTouchFocusing = true;
                }
            } catch (final Exception ignored) {
            }
        }
    }
}
 
Example 3
Source File: CameraPreview.java    From RecordVideo with Apache License 2.0 5 votes vote down vote up
public boolean manualFocus(Camera camera, Camera.AutoFocusCallback cb, List<Camera.Area> focusAreas
        ,List<Camera.Area> mFocusAreas) {
    //判断系统是否是4.0以上的版本
    if (camera != null && focusAreas != null && SystemVersionUtil.hasICS()) {
        try {
            camera.cancelAutoFocus();
            Camera.Parameters parameters = camera.getParameters();
            if(parameters != null){
                // getMaxNumFocusAreas检测设备是否支持
                if (parameters.getMaxNumFocusAreas() > 0) {
                    parameters.setFocusAreas(focusAreas);
                }
                // getMaxNumMeteringAreas检测设备是否支持
                if (parameters.getMaxNumMeteringAreas() > 0)
                    parameters.setMeteringAreas(mFocusAreas);
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
                camera.setParameters(parameters);
                camera.autoFocus(cb);
                return true;
            }
        } catch (Exception e) {
            if (e != null)
                Log.e(" ", "autoFocus", e);
        }
    }
    return false;
}
 
Example 4
Source File: CameraViewProxy.java    From Ti-Android-CameraView with MIT License 5 votes vote down vote up
@Kroll.method
public void snapPicture()
{
	Log.i(TAG, "Snap");
	Camera cam = ((CameraView) view).currentCameraInstance();
	
	if( isAutoFocusSupported() ) {
		cam.autoFocus(mAutoFocusCallback);
	} else {
		cam.takePicture(null, null, mPicture);
	}
}