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

The following examples show how to use android.hardware.Camera#setDisplayOrientation() . 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: CameraConfigurationManager.java    From LLApp with Apache License 2.0 6 votes vote down vote up
void setDesiredCameraParameters(Camera camera) {
	Camera.Parameters parameters = camera.getParameters();

	if (parameters == null) {
		if(AppConfig.DEBUG){
			Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
		}
		return;
	}

	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

	initializeTorch(parameters, prefs);
	String focusMode = findSettableValue(parameters.getSupportedFocusModes(), Camera.Parameters.FOCUS_MODE_AUTO,
			Camera.Parameters.FOCUS_MODE_MACRO);
	if (focusMode != null) {
		parameters.setFocusMode(focusMode);
	}

	parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
	/* 竖屏显示 */
	camera.setDisplayOrientation(90);
	camera.setParameters(parameters);
}
 
Example 2
Source File: CameraConfigurationManager.java    From QrModule with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the camera up to take preview images which are used for both preview and decoding.
 * We detect the preview format here so that buildLuminanceSource() can build an appropriate
 * LuminanceSource subclass. In the future we may want to force YUV420SP as it's the smallest,
 * and the planar Y can be used for barcode scanning without a copy in some cases.
 */
void setDesiredCameraParameters(Camera camera) {
    Camera.Parameters parameters = camera.getParameters();
    Log.d(TAG, "Setting preview size: " + cameraResolution);
    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    setFlash(parameters);
    setZoom(parameters);
    //setSharpness(parameters);
    //modify here
    camera.setDisplayOrientation(90);
    camera.setParameters(parameters);
}
 
Example 3
Source File: CameraConfigurationManager.java    From YZxing with Apache License 2.0 5 votes vote down vote up
void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) {

        Camera theCamera = camera.getCamera();
        Camera.Parameters parameters = theCamera.getParameters();

        if (parameters == null) {
            Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
            return;
        }

        Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

        if (safeMode) {
            Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        CameraConfigurationUtils.setFocus(
                parameters,
                prefs.getBoolean(Constant.KEY_AUTO_FOCUS, true),
                prefs.getBoolean(Constant.KEY_DISABLE_CONTINUOUS_FOCUS, true),
                safeMode);

        parameters.setPreviewSize(bestPreviewSize.x, bestPreviewSize.y);

        theCamera.setParameters(parameters);
        //设置屏幕方向为垂直方向
        theCamera.setDisplayOrientation(90);
    }
 
Example 4
Source File: CameraConfigurationManager.java    From zxing with MIT License 5 votes vote down vote up
void setDesiredCameraParameters(Camera camera) {
    Camera.Parameters parameters = camera.getParameters();
    Log.d(TAG, "Setting preview size: " + cameraResolution);
    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);

    setZoom(parameters);
    //setSharpness(parameters);
    //modify here
    camera.setDisplayOrientation(90);
    camera.setParameters(parameters);
}
 
Example 5
Source File: CameraConfigurationManager.java    From KSYMediaPlayer_Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the camera up to take preview images which are used for both preview and decoding.
 * We detect the preview format here so that buildLuminanceSource() can build an appropriate
 * LuminanceSource subclass. In the future we may want to force YUV420SP as it's the smallest,
 * and the planar Y can be used for barcode scanning without a copy in some cases.
 */
void setDesiredCameraParameters(Camera camera) {
  Camera.Parameters parameters = camera.getParameters();
  Log.d(TAG, "Setting preview size: " + cameraResolution);
  parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
  setFlash(parameters);
  setZoom(parameters);
  //setSharpness(parameters);
  //modify here
  camera.setDisplayOrientation(90);
  camera.setParameters(parameters);


}
 
Example 6
Source File: CameraConfigurationManager.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
public void setDesiredCameraParameters(Camera camera) {
		Camera.Parameters parameters = camera.getParameters();

		if (parameters == null) {
			Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
			return;
		}

		Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

//		if (safeMode) {
//			Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
//		}

		parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
		camera.setParameters(parameters);

		Camera.Parameters afterParameters = camera.getParameters();
		Camera.Size afterSize = afterParameters.getPreviewSize();
		if (afterSize != null && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
			Log.w(TAG, "Camera said it supported preview size " + cameraResolution.x + 'x' + cameraResolution.y + ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height);
			cameraResolution.x = afterSize.width;
			cameraResolution.y = afterSize.height;
		}

		/** 设置相机预览为竖屏 */
		camera.setDisplayOrientation(90);
	}
 
Example 7
Source File: CameraConfigManager.java    From StarBarcode with Apache License 2.0 5 votes vote down vote up
/**
 * 设置预期的相机参数
 * @param camera
 */
void setDesiredCameraParameters(OpenCamera camera) {
    Camera theCamera = camera.getCamera();
    Camera.Parameters parameters = theCamera.getParameters();
    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    theCamera.setParameters(parameters);
    theCamera.setDisplayOrientation(cwNeededRotation);
}
 
Example 8
Source File: CameraConfigurationManager.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the camera up to take preview images which are used for both preview and decoding.
 * We detect the preview format here so that buildLuminanceSource() can build an appropriate
 * LuminanceSource subclass. In the future we may want to force YUV420SP as it's the smallest,
 * and the planar Y can be used for barcode scanning without a copy in some cases.
 */
void setDesiredCameraParameters(Camera camera) {
    Camera.Parameters parameters = camera.getParameters();
    Log.d(TAG, "Setting preview size: " + cameraResolution);
    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    setFlash(parameters);
    setZoom(parameters);
    //setSharpness(parameters);
    //modify here
    camera.setDisplayOrientation(90);
    camera.setParameters(parameters);
}
 
Example 9
Source File: CameraConfigurationManager.java    From GOpenSource_AppKit_Android_AS with MIT License 5 votes vote down vote up
public void setDesiredCameraParameters(Camera camera, boolean safeMode) {
	Camera.Parameters parameters = camera.getParameters();

	if (parameters == null) {
		Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
		return;
	}

	Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

	if (safeMode) {
		Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
	}

	parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
	camera.setParameters(parameters);

	Camera.Parameters afterParameters = camera.getParameters();
	Camera.Size afterSize = afterParameters.getPreviewSize();
	if (afterSize != null && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
		Log.w(TAG, "Camera said it supported preview size " + cameraResolution.x + 'x' + cameraResolution.y + ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height);
		cameraResolution.x = afterSize.width;
		cameraResolution.y = afterSize.height;
	}

	/** 设置相机预览为竖屏 */
	String model=android.os.Build.MODEL;
	if("Nexus 5X".equals(model)){
		camera.setDisplayOrientation(270);
	}else{
		camera.setDisplayOrientation(90);
	}

}
 
Example 10
Source File: CameraConfigurationManager.java    From Gizwits-SmartSocket_Android with MIT License 5 votes vote down vote up
public void setDesiredCameraParameters(Camera camera, boolean safeMode) {
	Camera.Parameters parameters = camera.getParameters();

	if (parameters == null) {
		Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
		return;
	}

	Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

	if (safeMode) {
		Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
	}

	parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
	camera.setParameters(parameters);

	Camera.Parameters afterParameters = camera.getParameters();
	Camera.Size afterSize = afterParameters.getPreviewSize();
	if (afterSize != null && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
		Log.w(TAG, "Camera said it supported preview size " + cameraResolution.x + 'x' + cameraResolution.y + ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height);
		cameraResolution.x = afterSize.width;
		cameraResolution.y = afterSize.height;
	}

	/** 设置相机预览为竖屏 */
	camera.setDisplayOrientation(90);
}
 
Example 11
Source File: CameraConfigurationManager.java    From gokit-android with MIT License 5 votes vote down vote up
public void setDesiredCameraParameters(Camera camera, boolean safeMode) {
	Camera.Parameters parameters = camera.getParameters();

	if (parameters == null) {
		Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
		return;
	}

	Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

	if (safeMode) {
		Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
	}

	parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
	camera.setParameters(parameters);

	Camera.Parameters afterParameters = camera.getParameters();
	Camera.Size afterSize = afterParameters.getPreviewSize();
	if (afterSize != null && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
		Log.w(TAG, "Camera said it supported preview size " + cameraResolution.x + 'x' + cameraResolution.y + ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height);
		cameraResolution.x = afterSize.width;
		cameraResolution.y = afterSize.height;
	}

	/** 设置相机预览为竖屏 */
	String model=android.os.Build.MODEL;
	if("Nexus 5X".equals(model)){
		camera.setDisplayOrientation(270);
	}else{
		camera.setDisplayOrientation(90);
	}

}
 
Example 12
Source File: CameraConfigurationManager.java    From AirFree-Client with GNU General Public License v3.0 5 votes vote down vote up
public void setDesiredCameraParameters(Camera camera, boolean safeMode) {
    Camera.Parameters parameters = camera.getParameters();

    if (parameters == null) {
        Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
        return;
    }

    Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

    if (safeMode) {
        Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
    }

    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    camera.setParameters(parameters);

    Camera.Parameters afterParameters = camera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize
            .height)) {
        Log.w(TAG, "Camera said it supported preview size " + cameraResolution.x + 'x' +
                cameraResolution.y + ", but after setting it, preview size is " + afterSize.width + 'x'
                + afterSize.height);
        cameraResolution.x = afterSize.width;
        cameraResolution.y = afterSize.height;
    }

    /** 设置相机预览为竖屏 */
    camera.setDisplayOrientation(90);
}
 
Example 13
Source File: CameraConfigurationManager.java    From BarcodeScanner with Apache License 2.0 4 votes vote down vote up
void setDesiredCameraParameters(Camera camera, boolean safeMode) {
	Camera.Parameters parameters = camera.getParameters();

	if (parameters == null) {
		Log.w(TAG,
				"Device error: no camera parameters are available. Proceeding without configuration.");
		return;
	}

	Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

	if (safeMode) {
		Log.w(TAG,
				"In camera config safe mode -- most settings will not be honored");
	}

	SharedPreferences prefs = PreferenceManager
			.getDefaultSharedPreferences(context);

	// 初始化闪光灯
	initializeTorch(parameters, prefs, safeMode);

	// 默认使用自动对焦
	String focusMode = findSettableValue(
			parameters.getSupportedFocusModes(),
			Camera.Parameters.FOCUS_MODE_AUTO);

	// Maybe selected auto-focus but not available, so fall through here:
	if (!safeMode && focusMode == null) {
		focusMode = findSettableValue(parameters.getSupportedFocusModes(),
				Camera.Parameters.FOCUS_MODE_MACRO,
				Camera.Parameters.FOCUS_MODE_EDOF);
	}
	if (focusMode != null) {
		parameters.setFocusMode(focusMode);
	}

	if (prefs.getBoolean(Config.KEY_INVERT_SCAN, false)) {
		String colorMode = findSettableValue(
				parameters.getSupportedColorEffects(),
				Camera.Parameters.EFFECT_NEGATIVE);
		if (colorMode != null) {
			parameters.setColorEffect(colorMode);
		}
	}

	parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
	camera.setParameters(parameters);

	Camera.Parameters afterParameters = camera.getParameters();
	Camera.Size afterSize = afterParameters.getPreviewSize();
	if (afterSize != null
			&& (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
		Log.w(TAG, "Camera said it supported preview size "
				+ cameraResolution.x + 'x' + cameraResolution.y
				+ ", but after setting it, preview size is "
				+ afterSize.width + 'x' + afterSize.height);
		cameraResolution.x = afterSize.width;
		cameraResolution.y = afterSize.height;
	}

	camera.setDisplayOrientation(90);
}
 
Example 14
Source File: CameraConfigurationManager.java    From Study_Android_Demo with Apache License 2.0 4 votes vote down vote up
void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) {

    Camera theCamera = camera.getCamera();
    Camera.Parameters parameters = theCamera.getParameters();

    if (parameters == null) {
      Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
      return;
    }

    Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

    if (safeMode) {
      Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    initializeTorch(parameters, prefs, safeMode);

    CameraConfigurationUtils.setFocus(
        parameters,
        prefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true),
        prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, true),
        safeMode);

    if (!safeMode) {
      if (prefs.getBoolean(PreferencesActivity.KEY_INVERT_SCAN, false)) {
        CameraConfigurationUtils.setInvertColor(parameters);
      }

      if (!prefs.getBoolean(PreferencesActivity.KEY_DISABLE_BARCODE_SCENE_MODE, true)) {
        CameraConfigurationUtils.setBarcodeSceneMode(parameters);
      }

      if (!prefs.getBoolean(PreferencesActivity.KEY_DISABLE_METERING, true)) {
        CameraConfigurationUtils.setVideoStabilization(parameters);
        CameraConfigurationUtils.setFocusArea(parameters);
        CameraConfigurationUtils.setMetering(parameters);
      }

    }

    parameters.setPreviewSize(bestPreviewSize.x, bestPreviewSize.y);

    theCamera.setParameters(parameters);

    theCamera.setDisplayOrientation(cwRotationFromDisplayToCamera);

    Camera.Parameters afterParameters = theCamera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null && (bestPreviewSize.x != afterSize.width || bestPreviewSize.y != afterSize.height)) {
      Log.w(TAG, "Camera said it supported preview size " + bestPreviewSize.x + 'x' + bestPreviewSize.y +
          ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height);
      bestPreviewSize.x = afterSize.width;
      bestPreviewSize.y = afterSize.height;
    }
  }
 
Example 15
Source File: CameraSource.java    From trust-wallet-android-source with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Calculates the correct rotation for the given camera id and sets the rotation in the
 * parameters.  It also sets the camera's display orientation and rotation.
 *
 * @param parameters the camera parameters for which to set the rotation
 * @param cameraId   the camera id to set rotation based on
 */
private void setRotation(Camera camera, Camera.Parameters parameters, int cameraId) {
    WindowManager windowManager =
            (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    int degrees = 0;
    int rotation = windowManager.getDefaultDisplay().getRotation();
    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;
        default:
            Log.e(TAG, "Bad rotation value: " + rotation);
    }

    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);

    int angle;
    int displayAngle;
    if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
        angle = (cameraInfo.orientation + degrees) % 360;
        displayAngle = (360 - angle); // compensate for it being mirrored
    } else {  // back-facing
        angle = (cameraInfo.orientation - degrees + 360) % 360;
        displayAngle = angle;
    }

    // This corresponds to the rotation constants in {@link Frame}.
    mRotation = angle / 90;

    camera.setDisplayOrientation(displayAngle);
    parameters.setRotation(angle);
}
 
Example 16
Source File: ScanShapeActivity.java    From DevUtils with Apache License 2.0 4 votes vote down vote up
/**
     * 检查摄像头权限
     */
    private void checkPermission() {
        // 摄像头权限
        String cameraPermission = Manifest.permission.CAMERA;
        // 判断是否允许权限
        if (PermissionUtils.isGranted(cameraPermission)) {
            try {
                // 打开摄像头
                Camera camera = CameraUtils.open();
                camera.setDisplayOrientation(90); // 设置竖屏显示
                cameraAssist.setCamera(camera);
                // 获取预览大小
                final Camera.Size size = cameraAssist.getCameraResolution();
                // 设置预览大小, 需要这样设置, 开闪光灯才不会闪烁
                Camera.Parameters parameters = camera.getParameters();
                parameters.setPreviewSize(size.width, size.height);
                camera.setParameters(parameters);
                // 开始预览
                cameraAssist.openDriver(vid_ass_surface.getHolder()).startPreview();
//                // 默认开启自动对焦, 设置不需要自动对焦
//                cameraAssist.setAutoFocus(false);
            } catch (Exception e) {
                DevLogger.eTag(mTag, e, "checkPermission - startPreview");
            }
        } else {
            ToastTintUtils.warning("需要摄像头权限预览");
            // 申请权限
            PermissionUtils.permission(cameraPermission).callBack(new PermissionUtils.PermissionCallBack() {
                @Override
                public void onGranted() {
                    // 刷新处理
                    checkPermission();
                }

                @Override
                public void onDenied(List<String> grantedList, List<String> deniedList, List<String> notFoundList) {
                    // 再次申请权限
                    checkPermission();
                }
            }).request(this);
        }
    }
 
Example 17
Source File: CameraSource.java    From android-vision with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the correct rotation for the given camera id and sets the rotation in the
 * parameters.  It also sets the camera's display orientation and rotation.
 *
 * @param parameters the camera parameters for which to set the rotation
 * @param cameraId   the camera id to set rotation based on
 */
private void setRotation(Camera camera, Camera.Parameters parameters, int cameraId) {
    WindowManager windowManager =
            (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int degrees = 0;
    int rotation = windowManager.getDefaultDisplay().getRotation();
    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;
        default:
            Log.e(TAG, "Bad rotation value: " + rotation);
    }

    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);

    int angle;
    int displayAngle;
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        angle = (cameraInfo.orientation + degrees) % 360;
        displayAngle = (360 - angle); // compensate for it being mirrored
    } else {  // back-facing
        angle = (cameraInfo.orientation - degrees + 360) % 360;
        displayAngle = angle;
    }

    // This corresponds to the rotation constants in {@link Frame}.
    this.rotation = angle / 90;

    camera.setDisplayOrientation(displayAngle);
    parameters.setRotation(angle);
}
 
Example 18
Source File: CameraConfigurationManager.java    From ScanZbar with Apache License 2.0 4 votes vote down vote up
public void setDesiredCameraParameters(Camera camera, boolean safeMode) {
    Camera.Parameters parameters = camera.getParameters();

    if (parameters == null) {
        Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
        return;
    }

    Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

    if (safeMode) {
        Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
    }


    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(context);

    // 默认使用自动对焦
    String focusMode = findSettableValue(
            parameters.getSupportedFocusModes(),
            Camera.Parameters.FOCUS_MODE_AUTO);

    // Maybe selected auto-focus but not available, so fall through here:
    if (!safeMode && focusMode == null) {
        focusMode = findSettableValue(parameters.getSupportedFocusModes(),
                Camera.Parameters.FOCUS_MODE_MACRO,
                Camera.Parameters.FOCUS_MODE_EDOF);
    }
    if (focusMode != null) {
        parameters.setFocusMode(focusMode);
    }

    if (prefs.getBoolean(Config.KEY_INVERT_SCAN, false)) {
        String colorMode = findSettableValue(
                parameters.getSupportedColorEffects(),
                Camera.Parameters.EFFECT_NEGATIVE);
        if (colorMode != null) {
            parameters.setColorEffect(colorMode);
        }
    }

    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    camera.setParameters(parameters);

    Camera.Parameters afterParameters = camera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
        Log.w(TAG, "Camera said it supported preview size " + cameraResolution.x + 'x' + cameraResolution.y + ", but after setting it, preview size is " + afterSize.width + 'x' + afterSize.height);
        cameraResolution.x = afterSize.width;
        cameraResolution.y = afterSize.height;
    }

    /** 设置相机预览为竖屏 */
    camera.setDisplayOrientation(90);
}
 
Example 19
Source File: CodeScanner.java    From code-scanner with MIT License 4 votes vote down vote up
private void initialize() {
    Camera camera = null;
    final CameraInfo cameraInfo = new CameraInfo();
    final int cameraId = mCameraId;
    if (cameraId == CAMERA_BACK || cameraId == CAMERA_FRONT) {
        final int numberOfCameras = Camera.getNumberOfCameras();
        final int facing = cameraId == CAMERA_BACK ? CameraInfo.CAMERA_FACING_BACK :
                CameraInfo.CAMERA_FACING_FRONT;
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == facing) {
                camera = Camera.open(i);
                mCameraId = i;
                break;
            }
        }
    } else {
        camera = Camera.open(cameraId);
        Camera.getCameraInfo(cameraId, cameraInfo);
    }
    if (camera == null) {
        throw new CodeScannerException("Unable to access camera");
    }
    final Parameters parameters = camera.getParameters();
    if (parameters == null) {
        throw new CodeScannerException("Unable to configure camera");
    }
    final int orientation = Utils.getDisplayOrientation(mContext, cameraInfo);
    final boolean portrait = Utils.isPortrait(orientation);
    final Point imageSize =
            Utils.findSuitableImageSize(parameters, portrait ? mHeight : mWidth,
                    portrait ? mWidth : mHeight);
    final int imageWidth = imageSize.getX();
    final int imageHeight = imageSize.getY();
    parameters.setPreviewSize(imageWidth, imageHeight);
    parameters.setPreviewFormat(ImageFormat.NV21);
    final Point previewSize = Utils.getPreviewSize(portrait ? imageHeight : imageWidth,
            portrait ? imageWidth : imageHeight, mWidth, mHeight);
    final List<String> focusModes = parameters.getSupportedFocusModes();
    final boolean autoFocusSupported = focusModes != null &&
            (focusModes.contains(Parameters.FOCUS_MODE_AUTO) ||
                    focusModes.contains(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE));
    if (!autoFocusSupported) {
        mAutoFocusEnabled = false;
    }
    final Point viewSize = new Point(mWidth, mHeight);
    if (autoFocusSupported && mAutoFocusEnabled) {
        Utils.setAutoFocusMode(parameters, mAutoFocusMode);
        final Rect frameRect = mScannerView.getFrameRect();
        if (frameRect != null) {
            Utils.configureDefaultFocusArea(parameters, frameRect, previewSize, viewSize,
                    imageWidth, imageHeight, orientation);
        }
    }
    final List<String> flashModes = parameters.getSupportedFlashModes();
    final boolean flashSupported =
            flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH);
    if (!flashSupported) {
        mFlashEnabled = false;
    }
    final int zoom = mZoom;
    if (zoom != 0) {
        Utils.setZoom(parameters, zoom);
    }
    Utils.configureFpsRange(parameters);
    Utils.configureSceneMode(parameters);
    Utils.configureVideoStabilization(parameters);
    camera.setParameters(parameters);
    camera.setDisplayOrientation(orientation);
    synchronized (mInitializeLock) {
        final Decoder decoder =
                new Decoder(mDecoderStateListener, mFormats, mDecodeCallback);
        mDecoderWrapper =
                new DecoderWrapper(camera, cameraInfo, decoder, imageSize, previewSize,
                        viewSize, orientation, autoFocusSupported, flashSupported);
        decoder.start();
        mInitialization = false;
        mInitialized = true;
    }
    mMainThreadHandler.post(new FinishInitializationTask(previewSize));
}
 
Example 20
Source File: CameraSource.java    From Document-Scanner with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Calculates the correct rotation for the given camera id and sets the rotation in the
 * parameters.  It also sets the camera's display orientation and rotation.
 *
 * @param parameters the camera parameters for which to set the rotation
 * @param cameraId   the camera id to set rotation based on
 */
private void setRotation(Camera camera, Camera.Parameters parameters, int cameraId) {
    WindowManager windowManager =
            (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    int degrees = 0;
    int rotation = windowManager.getDefaultDisplay().getRotation();
    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;
        default:
            Log.e(TAG, "Bad rotation value: " + rotation);
    }

    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);

    int angle;
    int displayAngle;
    if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
        angle = (cameraInfo.orientation + degrees) % 360;
        displayAngle = (360 - angle); // compensate for it being mirrored
    } else {  // back-facing
        angle = (cameraInfo.orientation - degrees + 360) % 360;
        displayAngle = angle;
    }

    // This corresponds to the rotation constants in {@link Frame}.
    mRotation = angle / 90;

    camera.setDisplayOrientation(displayAngle);
    parameters.setRotation(angle);
}