|
发表于 2017-12-19 15:37:42
只看该作者
地板
Activity中代码:
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private SurfaceView mSurfaceView;
private CameraManager mCameraManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
mSurfaceView = findViewById(R.id.surfaceview);
mSurfaceView.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
}
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//在打开摄像头时,你先查看一下设备上摄像头id列表:有可能只有一个,"1"的值:根据的你情况而定
mCameraManager.openCamera("1", new CameraDevice.StateCallback() {
@Override
public void onOpened(final CameraDevice camera) {
try {
// 创建预览需要的CaptureRequest.Builder
final CaptureRequest.Builder previewRequestBuilder =
camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
// 将mSurfaceView的surface作为CaptureRequest.Builder的目标
previewRequestBuilder.addTarget(holder.getSurface());
//如果holder.getSurface().isValid()为false,则会出错;因为你的画布无效导致的问题
if (holder.getSurface().isValid())
//创建相机捕获会话,用于预览的工作。
camera.createCaptureSession(Arrays.asList(holder.getSurface()), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
if (null == camera) {
return;
}
try {
//设置预览模式
previewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
// 自动对焦
previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// 打开闪光灯
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
//重复请求预览
cameraCaptureSession.setRepeatingRequest(previewRequestBuilder.build(), null, new Handler());
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
}
}, new Handler());
} catch (CameraAccessException pE) {
Log.e(TAG,pE.getMessage());
pE.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice camera) {
camera.close();
}
@Override
public void onError(CameraDevice camera, int error) {
camera.close();
}
}, new Handler());
}
} catch (CameraAccessException pE) {
pE.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
layout中源码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.firefly.camera2.MainActivity">
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
mainfest.xml中的源码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firefly.camera2">
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest> |
|