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

The following examples show how to use android.hardware.Camera#cancelAutoFocus() . 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: CameraManager.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private static void setTorchEnabled(final Camera camera, final boolean enabled) {
    final Camera.Parameters parameters = camera == null ? null : camera.getParameters();
    if (parameters == null) {
        return;
    }
    final List<String> supportedFlashModes = parameters.getSupportedFlashModes();
    if (supportedFlashModes != null) {
        final String flashMode;
        if (enabled) {
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH,
                    Camera.Parameters.FLASH_MODE_ON);
        } else {
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_OFF);
        }

        if (flashMode != null) {
            camera.cancelAutoFocus(); // autofocus can cause conflict

            parameters.setFlashMode(flashMode);
            camera.setParameters(parameters);
        }
    }
}
 
Example 2
Source File: CameraManager.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
private static void setTorchEnabled(final Camera camera, final boolean enabled) {
    final Camera.Parameters parameters = camera.getParameters();

    final List<String> supportedFlashModes = parameters.getSupportedFlashModes();
    if (supportedFlashModes != null) {
        final String flashMode;
        if (enabled)
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH,
                    Camera.Parameters.FLASH_MODE_ON);
        else
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_OFF);

        if (flashMode != null) {
            camera.cancelAutoFocus(); // autofocus can cause conflict

            parameters.setFlashMode(flashMode);
            camera.setParameters(parameters);
        }
    }
}
 
Example 3
Source File: CodeScanner.java    From code-scanner with MIT License 6 votes vote down vote up
private void stopPreviewInternal(final boolean internal) {
    try {
        final DecoderWrapper decoderWrapper = mDecoderWrapper;
        if (decoderWrapper != null) {
            final Camera camera = decoderWrapper.getCamera();
            camera.cancelAutoFocus();
            final Parameters parameters = camera.getParameters();
            if (!internal && decoderWrapper.isFlashSupported() && mFlashEnabled) {
                Utils.setFlashMode(parameters, Parameters.FLASH_MODE_OFF);
            }
            camera.setParameters(parameters);
            camera.setPreviewCallback(null);
            camera.stopPreview();
        }
    } catch (final Exception ignored) {
    }
    mStoppingPreview = false;
    mPreviewActive = false;
    mSafeAutoFocusing = false;
    mSafeAutoFocusAttemptsCount = 0;
}
 
Example 4
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 5
Source File: CameraManager.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private static void setTorchEnabled(final Camera camera, final boolean enabled)
{
    final Camera.Parameters parameters = camera.getParameters();

    final List<String> supportedFlashModes = parameters.getSupportedFlashModes();
    if (supportedFlashModes != null)
    {
        final String flashMode;
        if (enabled)
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH, Camera.Parameters.FLASH_MODE_ON);
        else
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_OFF);

        if (flashMode != null)
        {
            camera.cancelAutoFocus(); // autofocus can cause conflict

            parameters.setFlashMode(flashMode);
            camera.setParameters(parameters);
        }
    }
}
 
Example 6
Source File: CameraManager.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
private static void setTorchEnabled(final Camera camera, final boolean enabled) {
    final Camera.Parameters parameters = camera.getParameters();

    final List<String> supportedFlashModes = parameters.getSupportedFlashModes();
    if (supportedFlashModes != null) {
        final String flashMode;
        if (enabled)
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH,
                    Camera.Parameters.FLASH_MODE_ON);
        else
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_OFF);

        if (flashMode != null) {
            camera.cancelAutoFocus(); // autofocus can cause conflict

            parameters.setFlashMode(flashMode);
            camera.setParameters(parameters);
        }
    }
}
 
Example 7
Source File: CameraManager.java    From bither-android with Apache License 2.0 6 votes vote down vote up
private static void setTorchEnabled(final Camera camera,
                                    final boolean enabled) {
    final Camera.Parameters parameters = camera.getParameters();

    final List<String> supportedFlashModes = parameters
            .getSupportedFlashModes();
    if (supportedFlashModes != null) {
        final String flashMode;
        if (enabled)
            flashMode = findValue(supportedFlashModes,
                    Camera.Parameters.FLASH_MODE_TORCH,
                    Camera.Parameters.FLASH_MODE_ON);
        else
            flashMode = findValue(supportedFlashModes,
                    Camera.Parameters.FLASH_MODE_OFF);

        if (flashMode != null) {
            camera.cancelAutoFocus(); // autofocus can cause conflict

            parameters.setFlashMode(flashMode);
            camera.setParameters(parameters);
        }
    }
}
 
Example 8
Source File: CameraManager.java    From Conversations with GNU General Public License v3.0 6 votes vote down vote up
private static void setTorchEnabled(final Camera camera, final boolean enabled) {
    final Camera.Parameters parameters = camera.getParameters();

    final List<String> supportedFlashModes = parameters.getSupportedFlashModes();
    if (supportedFlashModes != null) {
        final String flashMode;
        if (enabled)
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH,
                    Camera.Parameters.FLASH_MODE_ON);
        else
            flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_OFF);

        if (flashMode != null) {
            camera.cancelAutoFocus(); // autofocus can cause conflict

            parameters.setFlashMode(flashMode);
            camera.setParameters(parameters);
        }
    }
}
 
Example 9
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 10
Source File: CodeScanner.java    From code-scanner with MIT License 5 votes vote down vote up
private void setAutoFocusEnabledInternal(final boolean autoFocusEnabled) {
    try {
        final DecoderWrapper decoderWrapper = mDecoderWrapper;
        if (decoderWrapper != null) {
            final Camera camera = decoderWrapper.getCamera();
            camera.cancelAutoFocus();
            mTouchFocusing = false;
            final Parameters parameters = camera.getParameters();
            final AutoFocusMode autoFocusMode = mAutoFocusMode;
            if (autoFocusEnabled) {
                Utils.setAutoFocusMode(parameters, autoFocusMode);
            } else {
                Utils.disableAutoFocus(parameters);
            }
            if (autoFocusEnabled) {
                final Rect frameRect = mScannerView.getFrameRect();
                if (frameRect != null) {
                    Utils.configureDefaultFocusArea(parameters, decoderWrapper, frameRect);
                }
            }
            camera.setParameters(parameters);
            if (autoFocusEnabled) {
                mSafeAutoFocusAttemptsCount = 0;
                mSafeAutoFocusing = false;
                if (autoFocusMode == AutoFocusMode.SAFE) {
                    scheduleSafeAutoFocusTask();
                }
            }
        }
    } catch (final Exception ignored) {
    }
}
 
Example 11
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 12
Source File: FaceUtil.java    From ViseFace with Apache License 2.0 4 votes vote down vote up
/**
 * 在摄像头启动前设置参数
 *
 * @param cameraPreview
 * @param faceDetector
 * @param camera
 * @param cameraId
 * @param width
 * @param height
 * @param <T>
 */
public static <T> int setCameraParams(CameraPreview cameraPreview, IFaceDetector<T> faceDetector, Camera camera,
                                      int cameraId, int width, int height) {
    if (camera == null) {
        return 0;
    }
    // 获取摄像头支持的pictureSize列表
    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> pictureSizeList = parameters.getSupportedPictureSizes();
    // 从列表中选择合适的分辨率
    Point pictureSize = FaceUtil.findBestResolution(pictureSizeList, new Point(width, height), true, 0.15f);
    // 根据选出的PictureSize重新设置SurfaceView大小
    parameters.setPictureSize(pictureSize.x, pictureSize.y);

    // 获取摄像头支持的PreviewSize列表
    List<Camera.Size> previewSizeList = parameters.getSupportedPreviewSizes();
    Point preSize = FaceUtil.findBestResolution(previewSizeList, new Point(width, height), false, 0.15f);
    parameters.setPreviewSize(preSize.x, preSize.y);

    float w = preSize.x;
    float h = preSize.y;
    float scale = 1.0f;
    int tempW = (int) (height * (h / w));
    int tempH = (int) (width * (w / h));
    if (cameraPreview != null) {
        if (tempW >= width) {
            cameraPreview.setLayoutParams(new FrameLayout.LayoutParams(tempW, height));
            scale = tempW / h;
        } else if (tempH >= height) {
            cameraPreview.setLayoutParams(new FrameLayout.LayoutParams(width, tempH));
            scale = tempH / w;
        } else {
            cameraPreview.setLayoutParams(new FrameLayout.LayoutParams(width, height));
        }
    }
    if (faceDetector != null) {
        faceDetector.setZoomRatio(5f * scale);
        faceDetector.setPreviewWidth((int) w);
        faceDetector.setPreviewHeight((int) h);
    }

    parameters.setJpegQuality(100);
    if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
        // 连续对焦
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    }
    camera.cancelAutoFocus();
    int displayOrientation = setCameraDisplayOrientation(cameraPreview.getContext(), faceDetector, camera, cameraId);
    camera.setParameters(parameters);
    return displayOrientation;
}
 
Example 13
Source File: CameraPreview.java    From RecordVideo with Apache License 2.0 4 votes vote down vote up
/**
     * 设置相机参数
     */
    public void setupCamera(int cameraId,Camera mCamera) {
        haveSize = false;
        currentCameraId = cameraId;
//        List<Camera.Size> previewSizes = mCamera.getParameters().getSupportedPreviewSizes();
//        Log.e("zyf","size:"+previewSizes.size());
//        for (int i = 0; i < previewSizes.size(); i++) {
//            Camera.Size psize = previewSizes.get(i);
//            Log.e("zyf", "PreviewSize,width: " + psize.width + " height: " + psize.height);
//        }
        // 设置相机方向
        CameraHelper.setCameraDisplayOrientation(activity, cameraId, mCamera);
        // 设置相机参数
        Camera.Parameters parameters = mCamera.getParameters();
        // 若应用就是用来录制视频的,不用拍照功能,设置RecordingHint可以加快录制启动速度
        // 问题:小米手机录制视频支持的Size和相机预览支持的Size不一样(其他类似的手机可能
        // 也存在这个问题),若设置了这个标志位,会使预览效果拉伸,但是开始录制视频,预览
        // 又恢复正常,暂时不知道是为什么
//        parameters.setRecordingHint(true);
        List<String> focusModes = parameters.getSupportedFocusModes();
        if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)){
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        }else if(focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)){
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        }
        List<String> whiteBalance = parameters.getSupportedWhiteBalance();
        if (whiteBalance.contains(Camera.Parameters.WHITE_BALANCE_AUTO)){
            parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
        }

//        parameters.setFocusAreas(null);
        parameters.setMeteringAreas(null);

        int PreviewWidth = 0;
        int PreviewHeight = 0;
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);//获取窗口的管理器
        Display display = wm.getDefaultDisplay();//获得窗口里面的屏幕
        PreviewWidth = display.getHeight();
        PreviewHeight = display.getWidth();
        Camera.Parameters mParameters  = mCamera.getParameters();
        // 选择合适的预览尺寸
        List<Camera.Size> sizeList = mParameters.getSupportedPreviewSizes();

        // 如果sizeList只有一个我们也没有必要做什么了,因为就他一个别无选择
        if (sizeList.size() > 1) {
            Iterator<Camera.Size> itor = sizeList.iterator();
            while (itor.hasNext()) {
                Camera.Size cur = itor.next();
                if (cur.width >= PreviewWidth
                        && cur.height >= PreviewHeight) {
                    PreviewWidth = cur.width;
                    PreviewHeight = cur.height;
                    haveSize = true;
                    break;
                }
            }
            if(!haveSize){// 如果未找到和屏幕适配的size则取最大一个
                Collections.sort(sizeList, new PreviewOrder());
                for (int i = 0; i < sizeList.size(); i++) {
                    Log.e("zyf","sort:"+sizeList.get(i).height);
                }
                PreviewWidth = sizeList.get(0).width;
                PreviewHeight = sizeList.get(0).height;
            }
        }
//        mVideoSize = CameraHelper.getCameraPreviewSizeForVideo(cameraId, mCamera);
//        parameters.setPreviewSize(mVideoSize.width, mVideoSize.height);
        parameters.setPreviewSize(PreviewWidth, PreviewHeight); //获得摄像区域的大小
        Log.e("zyf", "PreviewCurrentSize,width: " + PreviewWidth + " height: " + PreviewHeight);
        mCamera.setParameters(parameters);
        if(!focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)){
            mCamera.cancelAutoFocus();
        }
    }