Android如何显示前置摄像头(仅适用于镜子)
Android how to show front camera (for mirror only)
我想在仅用于镜子的布局中显示前置摄像头,我只想要那个,并且我可能在摄像头布局上有几个简单的按钮。
注意:我不想拍照或任何东西。只想展示镜子的前置摄像头。
有没有最好的方法来实现它。
我曾经尝试过下面的代码,但相机显示错误的旋转,类似于横向模式,图像被拉伸。
surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
private var mCamera:Camera? = null
override fun surfaceDestroyed(holder: SurfaceHolder) {
mCamera?.stopPreview()
mCamera?.release()
mCamera = null
}
override fun surfaceCreated(holder: SurfaceHolder) {
mCamera = getCameraInstance()
try {
mCamera?.setPreviewDisplay(holder)
} catch (exception: IOException) {
mCamera?.release()
mCamera = null
}
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int,
height: Int) {
mCamera?.startPreview()
}
})
fun getCameraInstance(): Camera? {
var c: Camera? = null
try {
c = openFrontFacingCamera()
} catch (e: Exception) {
}
return c
}
private fun openFrontFacingCamera(): Camera? {
var cameraCount = 0
var cam: Camera? = null
val cameraInfo = Camera.CameraInfo()
cameraCount = Camera.getNumberOfCameras()
for (camIdx in 0 until cameraCount) {
Camera.getCameraInfo(camIdx, cameraInfo)
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx)
} catch (e: RuntimeException) {
// Log.e(FragmentActivity.TAG, "Camera failed to open: " + e.localizedMessage)
}
}
}
return cam
}
以下代码片段有助于打开前置摄像头
private Camera openFrontFacingCamera() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx<cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam; }
setDisplayOrientation()
方法可用于旋转相机视图。试试下面的代码片段来管理相机方向。
int rotation = activity.getWindowManager().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 (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;
}
camera.setDisplayOrientation(result);
我想在仅用于镜子的布局中显示前置摄像头,我只想要那个,并且我可能在摄像头布局上有几个简单的按钮。 注意:我不想拍照或任何东西。只想展示镜子的前置摄像头。 有没有最好的方法来实现它。 我曾经尝试过下面的代码,但相机显示错误的旋转,类似于横向模式,图像被拉伸。
surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
private var mCamera:Camera? = null
override fun surfaceDestroyed(holder: SurfaceHolder) {
mCamera?.stopPreview()
mCamera?.release()
mCamera = null
}
override fun surfaceCreated(holder: SurfaceHolder) {
mCamera = getCameraInstance()
try {
mCamera?.setPreviewDisplay(holder)
} catch (exception: IOException) {
mCamera?.release()
mCamera = null
}
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int,
height: Int) {
mCamera?.startPreview()
}
})
fun getCameraInstance(): Camera? {
var c: Camera? = null
try {
c = openFrontFacingCamera()
} catch (e: Exception) {
}
return c
}
private fun openFrontFacingCamera(): Camera? {
var cameraCount = 0
var cam: Camera? = null
val cameraInfo = Camera.CameraInfo()
cameraCount = Camera.getNumberOfCameras()
for (camIdx in 0 until cameraCount) {
Camera.getCameraInfo(camIdx, cameraInfo)
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx)
} catch (e: RuntimeException) {
// Log.e(FragmentActivity.TAG, "Camera failed to open: " + e.localizedMessage)
}
}
}
return cam
}
以下代码片段有助于打开前置摄像头
private Camera openFrontFacingCamera() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx<cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam; }
setDisplayOrientation()
方法可用于旋转相机视图。试试下面的代码片段来管理相机方向。
int rotation = activity.getWindowManager().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 (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;
}
camera.setDisplayOrientation(result);