下面列出了android.hardware.Camera#CameraInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
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);
}
/**
* @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;
}
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;
}
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
}
/**
* 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;
}
/**
* 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;
}
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;
}
/**
* 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);
}
/**
* 摄像机是否支持前置拍照
* @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;
}
public VideoCaptureAndroid(int id, long native_capturer) {
this.id = id;
this.native_capturer = native_capturer;
this.info = new Camera.CameraInfo();
Camera.getCameraInfo(id, info);
}
@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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}