下面列出了怎么用android.hardware.Camera.CameraInfo的API类实例代码及写法,或者点击链接到github查看源代码。
public static int getCameraDisplayOrientation(@NonNull Activity activity,
@NonNull CameraInfo info)
{
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
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;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
return (360 - ((info.orientation + degrees) % 360)) % 360;
} else {
return (info.orientation - degrees + 360) % 360;
}
}
private int determineCameraId() {
final int cameraCount = Camera.getNumberOfCameras();
final CameraInfo cameraInfo = new CameraInfo();
// prefer back-facing camera
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
return i;
}
}
// fall back to front-facing camera
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
return i;
}
}
return -1;
}
/**
* Create characteristics for a legacy device by mapping the {@code parameters}
* and {@code info}
*
* @param parameters A string parseable by {@link Camera.Parameters#unflatten}
* @param info Camera info with camera facing direction and angle of orientation
* @return static camera characteristics for a camera device
*
* @throws NullPointerException if any of the args were {@code null}
*/
public static CameraCharacteristics createCharacteristics(String parameters,
android.hardware.CameraInfo info) {
checkNotNull(parameters, "parameters must not be null");
checkNotNull(info, "info must not be null");
checkNotNull(info.info, "info.info must not be null");
CameraMetadataNative m = new CameraMetadataNative();
mapCharacteristicsFromInfo(m, info.info);
Camera.Parameters params = Camera.getEmptyParameters();
params.unflatten(parameters);
mapCharacteristicsFromParameters(m, params);
if (DEBUG) {
Log.v(TAG, "createCharacteristics metadata:");
Log.v(TAG, "--------------------------------------------------- (start)");
m.dumpToLog();
Log.v(TAG, "--------------------------------------------------- (end)");
}
return new CameraCharacteristics(m);
}
public CameraSurfaceView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
context = ctx;
setEGLContextClientVersion(2);
setRenderer(this);
setRenderMode(RENDERMODE_WHEN_DIRTY);
// Find the total number of available cameras and the ID of the default camera
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
selectedCameraId = i;
}
}
}
public CameraSurfaceView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
context = ctx;
setEGLContextClientVersion(2);
setRenderer(this);
setRenderMode(RENDERMODE_WHEN_DIRTY);
// Find the total number of available cameras and the ID of the default camera
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
selectedCameraId = i;
}
}
}
public CameraSurfaceView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
context = ctx;
setEGLContextClientVersion(2);
setRenderer(this);
setRenderMode(RENDERMODE_WHEN_DIRTY);
// Find the total number of available cameras and the ID of the default camera
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
selectedCameraId = i;
}
}
}
public CameraSurfaceView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
context = ctx;
setEGLContextClientVersion(2);
setRenderer(this);
setRenderMode(RENDERMODE_WHEN_DIRTY);
// Find the total number of available cameras and the ID of the default camera
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
selectedCameraId = i;
}
}
}
public CameraSurfaceView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
context = ctx;
setEGLContextClientVersion(2);
setRenderer(this);
setRenderMode(RENDERMODE_WHEN_DIRTY);
// Find the total number of available cameras and the ID of the default camera
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
selectedCameraId = i;
}
}
}
private boolean tryCreateCamera(int facing) {
CameraInfo info = new CameraInfo();
for (int nCam = 0; nCam < Camera.getNumberOfCameras(); nCam++) {
Camera.getCameraInfo(nCam, info);
if (info.facing == facing) {
camera = Camera.open(nCam);
cameraInfo = info;
//Size size = choosePictureSize(camera.getParameters().getSupportedPictureSizes());
Camera.Parameters params = camera.getParameters();
params.setPictureFormat(ImageFormat.JPEG);
//params.setPictureSize(size.width,size.height);
//params.setJpegThumbnailSize(128,128);
//params.setPreviewSize(size.width/2,size.height/2);
if (this.getCameraDirection() == CameraInfo.CAMERA_FACING_BACK) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
camera.setParameters(params);
return true;
}
}
return false;
}
public static com.tiktokdemo.lky.tiktokdemo.record.camera.camera.utils.CameraInfo getCameraInfo(){
com.tiktokdemo.lky.tiktokdemo.record.camera.camera.utils.CameraInfo info = new com.tiktokdemo.lky.tiktokdemo.record.camera.camera.utils.CameraInfo();
try{
Size size = getPreviewSize();
CameraInfo cameraInfo = new CameraInfo();
mCameraInfo = cameraInfo;
Camera.getCameraInfo(cameraID, cameraInfo);
info.previewWidth = PREVIEW_WIDTH;
info.previewHeight = PREVIEW_HEIGHT;
info.orientation = cameraInfo.orientation;
info.isFront = cameraID == 1 ? true : false;
size = getPictureSize();
if (size != null){
info.pictureWidth = size.width;
info.pictureHeight = size.height;
}
if(camera != null){
info.flashMode = camera.getParameters().getFlashMode();
}
return info;
}catch (Exception e){
e.printStackTrace();
return info;
}
}
/**
* 获取相机方向
*
* @param cameraFacing
* @return
*/
public static int getCameraOrientation(int cameraFacing) {
Camera.CameraInfo info = new Camera.CameraInfo();
int cameraId = -1;
int numCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numCameras; i++) {
Camera.getCameraInfo(i, info);
if (info.facing == cameraFacing) {
cameraId = i;
break;
}
}
if (cameraId < 0) {
// no front camera, regard it as back camera
return 90;
} else {
return info.orientation;
}
}
/**
* 单输入接口(fuRenderToNV21Image)
*
* @param img NV21数据
* @param w
* @param h
* @return
*/
public int onDrawFrame(byte[] img, int w, int h) {
if (img == null || w <= 0 || h <= 0) {
Log.e(TAG, "onDrawFrame data null");
return 0;
}
prepareDrawFrame();
int flags = mInputImageFormat;
if (mCameraFacing != Camera.CameraInfo.CAMERA_FACING_FRONT) flags |= FU_ADM_FLAG_FLIP_X;
if (mNeedBenchmark) mFuCallStartTime = System.nanoTime();
int fuTex = faceunity.fuRenderToNV21Image(img, w, h, mFrameId++, mItemsArray, flags);
if (mNeedBenchmark) mOneHundredFrameFUTime += System.nanoTime() - mFuCallStartTime;
return fuTex;
}
/**
* 双输入接口(fuDualInputToTexture)(处理后的画面数据并不会回写到数组),由于省去相应的数据拷贝性能相对最优,推荐使用。
*
* @param img NV21数据
* @param tex 纹理ID
* @param w
* @param h
* @return
*/
public int onDrawFrame(byte[] img, int tex, int w, int h) {
if (tex <= 0 || img == null || w <= 0 || h <= 0) {
Log.e(TAG, "onDrawFrame data null");
return 0;
}
prepareDrawFrame();
int flags = mInputTextureType | mInputImageFormat;
if (mCameraFacing != Camera.CameraInfo.CAMERA_FACING_FRONT) flags |= FU_ADM_FLAG_FLIP_X;
if (mNeedBenchmark) mFuCallStartTime = System.nanoTime();
int fuTex = faceunity.fuDualInputToTexture(img, tex, flags, w, h, mFrameId++, mItemsArray);
if (mNeedBenchmark) mOneHundredFrameFUTime += System.nanoTime() - mFuCallStartTime;
return fuTex;
}
private int calculateRotModeLagacy() {
int mode;
if (mInputOrientation == 270) {
if (mCameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mode = mDeviceOrientation / 90;
} else {
mode = (mDeviceOrientation - 180) / 90;
}
} else {
if (mCameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mode = (mDeviceOrientation + 180) / 90;
} else {
mode = mDeviceOrientation / 90;
}
}
return mode;
}
private int determineCameraId() {
final int cameraCount = Camera.getNumberOfCameras();
final CameraInfo cameraInfo = new CameraInfo();
// prefer back-facing camera
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
return i;
}
// fall back to front-facing camera
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
return i;
}
return -1;
}
public void flipCamera() {
if (Camera.getNumberOfCameras() > 1) {
cameraId = cameraId == CameraInfo.CAMERA_FACING_BACK
? CameraInfo.CAMERA_FACING_FRONT
: CameraInfo.CAMERA_FACING_BACK;
onPause();
onResume();
TextSecurePreferences.setDirectCaptureCameraId(getContext(), cameraId);
}
}
private int getCameraPictureOrientation() {
if (getActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
outputOrientation = getCameraPictureRotation(getActivity().getWindowManager()
.getDefaultDisplay()
.getOrientation());
} else if (getCameraInfo().facing == CameraInfo.CAMERA_FACING_FRONT) {
outputOrientation = (360 - displayOrientation) % 360;
} else {
outputOrientation = displayOrientation;
}
return outputOrientation;
}
/**
* Gets the id for the camera specified by the direction it is facing. Returns -1 if no such
* camera was found.
*
* @param facing the desired camera (front-facing or rear-facing)
*/
private static int getIdForRequestedCamera(int facing) {
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == facing) {
return i;
}
}
return -1;
}
public int getCameraPictureRotation(int orientation) {
final CameraInfo info = getCameraInfo();
final int rotation;
orientation = (orientation + 45) / 90 * 90;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else {
rotation = (info.orientation + orientation) % 360;
}
return rotation;
}
@Override
protected byte[] doInBackground(byte[]... params) {
final byte[] data = params[0];
try {
return BitmapUtil.createFromNV21(data,
previewSize.width,
previewSize.height,
rotation,
croppingRect,
cameraId == CameraInfo.CAMERA_FACING_FRONT);
} catch (IOException e) {
Log.w(TAG, e);
return null;
}
}
/**
* Gets the id for the camera specified by the direction it is facing. Returns -1 if no such
* camera was found.
*
* @param facing the desired camera (front-facing or rear-facing)
*/
private static int getIdForRequestedCamera(int facing) {
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == facing) {
return i;
}
}
return -1;
}
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:
}
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) % 360; // compensate for it being mirrored
} else { // back-facing
angle = (cameraInfo.orientation - degrees + 360) % 360;
displayAngle = angle;
}
mRotation = angle / 90;
camera.setDisplayOrientation(displayAngle);
parameters.setRotation(angle);
}
/**
* Gets the id for the camera specified by the direction it is facing. Returns -1 if no such
* camera was found.
*
* @param facing the desired camera (front-facing or rear-facing)
*/
private static int getIdForRequestedCamera(int facing) {
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == facing) {
return i;
}
}
return -1;
}
/**
* Create characteristics for a legacy device by mapping the {@code parameters}
* and {@code info}
*
* @param parameters A non-{@code null} parameters set
* @param info Camera info with camera facing direction and angle of orientation
*
* @return static camera characteristics for a camera device
*
* @throws NullPointerException if any of the args were {@code null}
*/
public static CameraCharacteristics createCharacteristics(Camera.Parameters parameters,
CameraInfo info) {
checkNotNull(parameters, "parameters must not be null");
checkNotNull(info, "info must not be null");
String paramStr = parameters.flatten();
android.hardware.CameraInfo outerInfo = new android.hardware.CameraInfo();
outerInfo.info = info;
return createCharacteristics(paramStr, outerInfo);
}
@SuppressLint("NewApi")
private int getCameraId(int cameraId) {
CameraInfo ci = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, ci);
if (ci.facing == cameraId) { //CameraInfo.CAMERA_FACING_FRONT) {
MLog.d(TAG, "returning " + i);
return i;
//} else (ci.facing == CameraInfo.CAMERA_FACING_BACK) {
// return i;
}
}
return -1; // No front-facing camera found
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (modeCamera == MODE_CAMERA_FRONT) {
cameraId = getCameraId(CameraInfo.CAMERA_FACING_FRONT);
MLog.d(TAG, "" + cameraId);
if (cameraId == -1) {
MLog.d(TAG, "there is no camera");
}
mCamera = Camera.open(cameraId);
} else {
cameraId = 0;
mCamera = Camera.open();
}
mParameters = mCamera.getParameters();
// SizePair siz = generateValidPreviewSize(camera, 480, 640);
// width = siz.mPicture.getWidth();
// height = siz.mPicture.getHeight();
mParameters.setPreviewSize(640, 480);
try {
applyParameters();
mCamera.setPreviewTexture(surface);
} catch (IOException exception) {
mCamera.release();
}
setCameraDisplayOrientation(cameraId, mCamera);
mCamera.startPreview();
mOnReadyCallback.event();
}
public int setCameraDisplayOrientation() {
if (camera == null || cameraInfo == null) {
return -1;
}
WindowManager winManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int rotation = winManager.getDefaultDisplay().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;
}
int result;
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
result = (cameraInfo.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (cameraInfo.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
return result;
}
/**
* 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.
*/
private void setRotation(Camera camera, Camera.Parameters parameters) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int deviceRotation = windowManager.getDefaultDisplay().getRotation();
int degrees = 0;
switch (deviceRotation) {
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 device rotation value: " + deviceRotation);
}
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(CAMERA_FACING_BACK, cameraInfo);
int angle = (cameraInfo.orientation - degrees + 360) % 360;
// This corresponds to the rotation constants in FirebaseVisionImageMetadata.
this.rotation = angle / 90;
camera.setDisplayOrientation(angle);
parameters.setRotation(angle);
}
/**
* Gets the id for the camera specified by the direction it is facing. Returns -1 if no such
* camera was found.
*
* @param facing the desired camera (front-facing or rear-facing)
*/
private static int getIdForRequestedCamera(int facing) {
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == facing) {
return i;
}
}
return -1;
}
/**
* Gets the id for the camera specified by the direction it is facing. Returns -1 if no such
* camera was found.
*
* @param facing the desired camera (front-facing or rear-facing)
*/
private static int getIdForRequestedCamera(int facing) {
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == facing) {
return i;
}
}
return -1;
}