Java Code Examples for android.hardware.Camera#CameraInfo

The following examples show how to use android.hardware.Camera#CameraInfo . 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: CameraHelper.java    From ScreenCapture with MIT License 6 votes vote down vote up
/**
 * @param position Physical position of the camera i.e Camera.CameraInfo.CAMERA_FACING_FRONT
 *                 or Camera.CameraInfo.CAMERA_FACING_BACK.
 * @return the default camera on the device. Returns null if camera is not available.
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static Camera getDefaultCamera(int position) {
    // Find the total number of cameras available
    int mNumberOfCameras = Camera.getNumberOfCameras();

    // Find the ID of the back-facing ("default") camera
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    for (int i = 0; i < mNumberOfCameras; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == position) {
            return Camera.open(i);

        }
    }

    return null;
}
 
Example 2
Source File: DocumentScannerActivity.java    From Document-Scanner with GNU General Public License v3.0 6 votes vote down vote up
private int findBestCamera() {
    int cameraId = -1;
    //Search for the back facing camera
    //get the number of cameras
    int numberOfCameras = Camera.getNumberOfCameras();
    //for every camera check
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            cameraId = i;
            break;
        }
        cameraId = i;
    }
    return cameraId;
}
 
Example 3
Source File: ScannerManager.java    From attendee-checkin with Apache License 2.0 6 votes vote down vote up
private Camera openCamera() {
    Camera.CameraInfo info = new Camera.CameraInfo();
    int id = chooseBackFacingCamera(info);
    Camera camera = Camera.open(id);
    Point screenResolution = new Point();
    camera.setDisplayOrientation((info.orientation - getDisplayInfo(screenResolution) + 360) % 360);
    Camera.Parameters parameters = camera.getParameters();
    CameraConfigurationUtils.setFocus(parameters, true, true, false);
    CameraConfigurationUtils.setBarcodeSceneMode(parameters);
    CameraConfigurationUtils.setVideoStabilization(parameters);
    CameraConfigurationUtils.setFocusArea(parameters);
    CameraConfigurationUtils.setMetering(parameters);
    mCaptureSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
    Log.d(TAG, "Screen resolution: " + screenResolution.x + "x" + screenResolution.y);
    Log.d(TAG, "Preview size: " + mCaptureSize.x + "x" + mCaptureSize.y);
    parameters.setPreviewSize(mCaptureSize.x, mCaptureSize.y);
    camera.setParameters(parameters);
    return camera;
}
 
Example 4
Source File: MultiTexOffScreenActivity.java    From android-openGL-canvas with Apache License 2.0 6 votes vote down vote up
private void openCamera() {
    Camera.CameraInfo info = new Camera.CameraInfo();

    // Try to find a front-facing camera (e.g. for videoconferencing).
    int numCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numCameras; i++) {
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            mCamera = Camera.open(i);
            break;
        }
    }
    if (mCamera == null) {
        mCamera = Camera.open();    // opens first back-facing camera
    }

    Camera.Parameters parms = mCamera.getParameters();

    CameraUtils.choosePreviewSize(parms, 1280, 720);
}
 
Example 5
Source File: CameraInterface.java    From CameraView with Apache License 2.0 5 votes vote down vote up
private void findAvailableCameras() {
    Camera.CameraInfo info = new Camera.CameraInfo();
    int cameraNum = Camera.getNumberOfCameras();
    for (int i = 0; i < cameraNum; i++) {
        Camera.getCameraInfo(i, info);
        switch (info.facing) {
            case Camera.CameraInfo.CAMERA_FACING_FRONT:
                CAMERA_FRONT_POSITION = info.facing;
                break;
            case Camera.CameraInfo.CAMERA_FACING_BACK:
                CAMERA_POST_POSITION = info.facing;
                break;
        }
    }
}
 
Example 6
Source File: CameraUtils.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 摄像机是否支持前置拍照
 * @return
 */
static boolean isSupportFrontCamera() {
    final int cameraCount = Camera.getNumberOfCameras();
    Camera.CameraInfo info = new Camera.CameraInfo();
    for (int i = 0; i < cameraCount; i++) {
        Camera.getCameraInfo(i, info);
        if (info.facing == 1) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: CameraGLView.java    From TimeLapseRecordingSample with Apache License 2.0 5 votes vote down vote up
/**
		 * rotate preview screen according to the device orientation
		 * @param params
		 */
		private final void setRotation(Camera.Parameters params) {
			if (DEBUG) Log.v(TAG, "setRotation:");

			final Display display = ((WindowManager)getContext()
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
			int rotation = display.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;
			}
			// get whether the camera is front camera or back camera
			final Camera.CameraInfo info =
					new android.hardware.Camera.CameraInfo();
				android.hardware.Camera.getCameraInfo(CAMERA_ID, info);
			mIsFrontFace = (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT);
			if (mIsFrontFace) {	// front camera
				degrees = (info.orientation + degrees) % 360;
				degrees = (360 - degrees) % 360;  // reverse
			} else {  // back camera
				degrees = (info.orientation - degrees + 360) % 360;
			}
			// apply rotation setting
			mCamera.setDisplayOrientation(degrees);
			mRotation = degrees;
			// XXX This method fails to call and camera stops working on some devices.
//			params.setRotation(degrees);
		}
 
Example 8
Source File: CameraUtil.java    From Expert-Android-Programming with MIT License 5 votes vote down vote up
public static int getFrontCameraId() {
    if (frontCameraId == -1) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < getCameraNumber(); i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                frontCameraId = i;
                break;
            }
        }
    }
    return frontCameraId;
}
 
Example 9
Source File: GingerbreadOpenCameraInterface.java    From AndroidWebServ with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a rear-facing camera with {@link Camera#open(int)}, if one exists, or opens camera 0.
 */
@Override
public Camera open() {

    int numCameras = Camera.getNumberOfCameras();
    if (numCameras == 0) {
        Log.w(TAG, "No cameras!");
        return null;
    }

    int index = 0;
    while (index < numCameras) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(index, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            break;
        }
        index++;
    }

    Camera camera;
    if (index < numCameras) {
        Log.i(TAG, "Opening camera #" + index);
        camera = Camera.open(index);
    } else {
        Log.i(TAG, "No camera facing back; returning camera #0");
        camera = Camera.open(0);
    }

    return camera;
}
 
Example 10
Source File: CameraPreview.java    From android-espresso-revealed with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the correct orientation for a {@link Camera} preview that is displayed on screen.
 * <p>
 * Implementation is based on the sample code provided in
 * {@link Camera#setDisplayOrientation(int)}.
 */
public static int calculatePreviewOrientation(Camera.CameraInfo info, int rotation) {
    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;
    }

    return result;
}
 
Example 11
Source File: VideoStream.java    From VideoMeeting with Apache License 2.0 5 votes vote down vote up
protected synchronized boolean createCamera(SurfaceHolder holder) throws RuntimeException {
        if (mSurfaceView == null)
            throw new RuntimeException("Invalid surface !");
//        if (mSurfaceView.getHolder() == null || !mSurfaceReady)
        if (mSurfaceView.getHolder() == null)
            throw new RuntimeException("Invalid surface !");
        try {
            mCamera = Camera.open(mCameraId);
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); //自动对焦
            int[] max = Util.determineMaximumSupportedFramerate(parameters);
            Camera.CameraInfo camInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(mCameraId, camInfo);
            int cameraRotationOffset = camInfo.orientation;
            int rotate = (360 + cameraRotationOffset - getDgree()) % 360;
            parameters.setRotation(rotate); //设置旋转角度exif信息
            parameters.setPreviewFormat(ImageFormat.NV21);//设置预览图片格式为NV21
            List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
            // TODO 设置为手机支持的宽高
            parameters.setPreviewSize(width, height);//设置预览宽高
            parameters.setPreviewFpsRange(max[0], max[1]);
            mCamera.setParameters(parameters);
            int displayRotation;
            displayRotation = (cameraRotationOffset - getDgree() + 360) % 360;
            mCamera.setDisplayOrientation(displayRotation);//设置相机旋转角度
            mCamera.setPreviewDisplay(holder);
            return true;
        } catch (Exception e) {
            destroyCamera();
            return false;
        }
    }
 
Example 12
Source File: CameraUtil.java    From RxCamera with MIT License 5 votes vote down vote up
public static int getBackCameraId() {
    if (backCameraId == -1) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < getCameraNumber(); i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                backCameraId = i;
                break;
            }
        }
    }
    return backCameraId;
}
 
Example 13
Source File: CameraHelper.java    From AndroidRecording with Apache License 2.0 5 votes vote down vote up
public static int setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera, int displayRotation) {
	Camera.CameraInfo info = new Camera.CameraInfo();
	Camera.getCameraInfo(cameraId, info);
	int degrees = 0;
	switch (displayRotation) {
		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 camRotationDegree = 0;
	if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
		camRotationDegree = (info.orientation + degrees) % 360;
		camRotationDegree = (360 - camRotationDegree) % 360; // compensate the mirror
	} else { 
		camRotationDegree = (info.orientation - degrees + 360) % 360;
	}

	if (camera != null) {
		camera.setDisplayOrientation(camRotationDegree);
	}
	return camRotationDegree;
}
 
Example 14
Source File: CameraSession.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
protected void configureRecorder(int quality, MediaRecorder recorder) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraInfo.cameraId, info);
    int displayOrientation = getDisplayOrientation(info, false);


    int outputOrientation = 0;
    if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            outputOrientation = (info.orientation - jpegOrientation + 360) % 360;
        } else {
            outputOrientation = (info.orientation + jpegOrientation) % 360;
        }
    }
    recorder.setOrientationHint(outputOrientation);

    int highProfile = getHigh();
    boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
    boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
    if (canGoHigh && (quality == 1 || !canGoLow)) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
    } else if (canGoLow) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
    } else {
        throw new IllegalStateException("cannot find valid CamcorderProfile");
    }
    isVideo = true;
}
 
Example 15
Source File: CameraSession.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public int getDisplayOrientation() {
    try {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraInfo.getCameraId(), info);
        return getDisplayOrientation(info, true);
    } catch (Exception e) {
        FileLog.e(e);
    }
    return 0;
}
 
Example 16
Source File: OpenCameraInterface.java    From Gizwits-SmartBuld_Android with MIT License 4 votes vote down vote up
/**
 * Opens the requested camera with {@link Camera#open(int)}, if one exists.
 * 
 * @param cameraId
 *            camera ID of the camera to use. A negative value means
 *            "no preference"
 * @return handle to {@link Camera} that was opened
 */
public static Camera open(int cameraId) {

	int numCameras = Camera.getNumberOfCameras();
	if (numCameras == 0) {
		Log.w(TAG, "No cameras!");
		return null;
	}

	boolean explicitRequest = cameraId >= 0;

	if (!explicitRequest) {
		// Select a camera if no explicit camera requested
		int index = 0;
		while (index < numCameras) {
			Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
			Camera.getCameraInfo(index, cameraInfo);
			if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
				break;
			}
			index++;
		}

		cameraId = index;
	}

	Camera camera;
	if (cameraId < numCameras) {
		Log.i(TAG, "Opening camera #" + cameraId);
		camera = Camera.open(cameraId);
	} else {
		if (explicitRequest) {
			Log.w(TAG, "Requested camera does not exist: " + cameraId);
			camera = null;
		} else {
			Log.i(TAG, "No camera facing back; returning camera #0");
			camera = Camera.open(0);
		}
	}

	return camera;
}
 
Example 17
Source File: OpenCameraInterface.java    From zxing with MIT License 4 votes vote down vote up
/**
   * Opens the requested camera with {@link Camera#open(int)}, if one exists.
   *
   * @param cameraId camera ID of the camera to use. A negative value means "no preference"
   * @return handle to {@link Camera} that was opened
   */
  @SuppressLint("NewApi")
public static Camera open(int cameraId) {
    
    int numCameras = Camera.getNumberOfCameras();
    if (numCameras == 0) {
      Log.w(TAG, "No cameras!");
      return null;
    }

    boolean explicitRequest = cameraId >= 0;

    if (!explicitRequest) {
      // Select a camera if no explicit camera requested
      int index = 0;
      while (index < numCameras) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(index, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
          break;
        }
        index++;
      }
      
      cameraId = index;
    }

    Camera camera;
    if (cameraId < numCameras) {
      Log.i(TAG, "Opening camera #" + cameraId);
      camera = Camera.open(cameraId);
    } else {
      if (explicitRequest) {
        Log.w(TAG, "Requested camera does not exist: " + cameraId);
        camera = null;
      } else {
        Log.i(TAG, "No camera facing back; returning camera #0");
        camera = Camera.open(0);
      }
    }
    
    return camera;
  }
 
Example 18
Source File: OpenCameraInterface.java    From Gizwits-SmartSocket_Android with MIT License 4 votes vote down vote up
/**
 * Opens the requested camera with {@link Camera#open(int)}, if one exists.
 * 
 * @param cameraId
 *            camera ID of the camera to use. A negative value means
 *            "no preference"
 * @return handle to {@link Camera} that was opened
 */
public static Camera open(int cameraId) {

	int numCameras = Camera.getNumberOfCameras();
	if (numCameras == 0) {
		Log.w(TAG, "No cameras!");
		return null;
	}

	boolean explicitRequest = cameraId >= 0;

	if (!explicitRequest) {
		// Select a camera if no explicit camera requested
		int index = 0;
		while (index < numCameras) {
			Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
			Camera.getCameraInfo(index, cameraInfo);
			if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
				break;
			}
			index++;
		}

		cameraId = index;
	}

	Camera camera;
	if (cameraId < numCameras) {
		Log.i(TAG, "Opening camera #" + cameraId);
		camera = Camera.open(cameraId);
	} else {
		if (explicitRequest) {
			Log.w(TAG, "Requested camera does not exist: " + cameraId);
			camera = null;
		} else {
			Log.i(TAG, "No camera facing back; returning camera #0");
			camera = Camera.open(0);
		}
	}

	return camera;
}
 
Example 19
Source File: CameraBaseActivity.java    From CameraV with GNU General Public License v3.0 4 votes vote down vote up
@SuppressLint("NewApi")
private boolean tryCreateCamera(int facing)
{
     Camera.CameraInfo info = new Camera.CameraInfo();
     for (int nCam = 0; nCam < Camera.getNumberOfCameras(); nCam++)
     {
	     Camera.getCameraInfo(nCam, info);
	     if (info.facing == facing)
	     {
			 try {

				 camera = Camera.open(nCam);

				 cameraInfo = info;

				 Camera.Parameters params = camera.getParameters();
				 params.setPictureFormat(ImageFormat.JPEG);

				 mRotation = setCameraDisplayOrientation(this,nCam,camera);

				 List<Camera.Size> supportedPreviewSizes =  camera.getParameters().getSupportedPreviewSizes();
				 List<Camera.Size> supportedPictureSize = camera.getParameters().getSupportedPictureSizes();

				 int previewQuality = Math.min(supportedPreviewSizes.size()-1,3);

				 if (mPreviewWidth == -1)
					 mPreviewWidth = supportedPreviewSizes.get(previewQuality).width;

				 if (mPreviewHeight == -1)
					 mPreviewHeight = supportedPreviewSizes.get(previewQuality).height;

				 params.setPreviewSize(mPreviewWidth, mPreviewHeight);

				 params.setPictureSize(supportedPictureSize.get(1).width, supportedPictureSize.get(1).height);

				 if (this.getCameraDirection() == CameraInfo.CAMERA_FACING_BACK)
				 {
					 if (getPackageManager().hasSystemFeature(
								PackageManager.FEATURE_CAMERA_AUTOFOCUS))
							if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
								params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
							}


					 if (mRotation > 0)
						 params.setRotation(mRotation);

					 /**
					 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
						 if (params.isVideoStabilizationSupported())
							 params.setVideoStabilization(true);

					 }*/

				 }
				 else
				 {
					 if (mRotation > 0)
						 params.setRotation(360-mRotation);
				 }

				 camera.setParameters(params);

					/*
					for (int i = 0; i < BUFFER_COUNT; i++) {
						byte[] buffer = new byte[mPreviewWidth * mPreviewHeight *
							ImageFormat.getBitsPerPixel(ImageFormat.NV21) / 8];
						camera.addCallbackBuffer(buffer);

					}

//					camera.setPreviewCallback(this);
					camera.setPreviewCallbackWithBuffer(this);
					*/
					camera.setPreviewCallback(this);

					if (holder != null)
						try {
							camera.setPreviewDisplay(holder);
							camera.startPreview();
							mPreviewing = true;
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					//start video

				 return true;

			 }
			 catch (Exception re)
			 {
				 Log.e("Camera","unable to open camera",re);
				 return false;
			 }
	     }
     }
     return false;
}
 
Example 20
Source File: VideoCaptureAndroid.java    From webrtc-app-mono with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public VideoCaptureAndroid(int id, long native_capturer) {
  this.id = id;
  this.native_capturer = native_capturer;
  this.info = new Camera.CameraInfo();
  Camera.getCameraInfo(id, info);
}