80,350
社区成员
发帖
与我相关
我的任务
分享
final SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int numCameras = Camera.getNumberOfCameras();
if (numCameras == 0) {
return;
}
Camera.CameraInfo selectedCameraInfo = null;
int index;
index = 0;
while (index < numCameras) {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(index, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
selectedCameraInfo = cameraInfo;
break;
}
index++;
}
Camera camera;
if (index < numCameras) {
camera = Camera.open(index);
} else {
camera = Camera.open(0);
selectedCameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(0, selectedCameraInfo);
}
if (camera == null) {
return ;
} else {
try {
camera.setPreviewDisplay(surfaceView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
}
});
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/button1"
android:layout_gravity="center_horizontal"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="打开摄像头"/>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
希望能帮助你们~Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("camerasensortype", 2); // 调用前置摄像头
//intent.putExtra("autofocus", true); // 自动对焦 去掉这句吧
intent.putExtra("fullScreen", false); // 全屏
intent.putExtra("showActionIcons", false);
startActivityForResult(intent, 1);
再试下?