【文件属性】:
文件名称:Android 调用前摄像头拍照存储,根据打开时横竖屏切换preview
文件大小:628KB
文件格式:RAR
更新时间:2017-06-24 11:02:25
android 前摄像头 横竖屏预览
android调用camera时,可以自己写一个activity,赋上相关参数,打开前camera就可以了;
需要申请的permission,在AndroidManifest.xml中添加:
主要功能,打开前camera
private Camera openFrontFacingCameraGingerbread() {
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);
mCurrentCamIndex = camIdx;
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
}
根据打开时的横竖屏方向来调整preview角度
//根据横竖屏自动调节preview方向,Starting from API level 14, this method can be called when preview is active.
private static void setCameraDisplayOrientation(Activity activity,int cameraId, Camera camera)
{
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
//degrees the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270.
//The starting position is 0 (landscape).
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);
}
【文件预览】:
bin
----classes()
res
----drawable-ldpi()
----values-v11()
--------styles.xml(324B)
----menu()
--------main.xml(439B)
----values-v14()
--------styles.xml(381B)
----drawable-hdpi()
--------ic_launcher.png(7KB)
----drawable-xhdpi()
--------ic_launcher.png(12KB)
----drawable-xxhdpi()
--------ic_launcher.png(24KB)
----values()
--------strings.xml(273B)
--------styles.xml(680B)
--------dimens.xml(213B)
----values-sw720dp-land()
--------dimens.xml(269B)
----drawable-mdpi()
--------ic_launcher.png(4KB)
----values-sw600dp()
--------dimens.xml(196B)
----layout()
--------activity_main.xml(1KB)
proguard-project.txt
ic_launcher-web.png
assets
gen
----com()
--------yxiaolv()
src
----com()
--------yxiaolv()
.project
.classpath
project.properties
README.md
AndroidManifest.xml
libs
----android-support-v4.jar(613KB)