相信大家都看过不少博主的打开相机等操作,我也是看过不少的博主写的文章,但是基本上很多都是比较旧的内容,大概都是用Camera这个类打开。 今天我写的文章是使用camera2来打开相机,并用SurfaceView预览相机拍摄内容。 首先,使用摄像头是需要摄像头的权限: 在AndroidManifest.xml文件添加权限:
<uses-permission android:name="android.permission.CAMERA" />
然后在代码里面申请权限,当然,6.0以下的是不需要添加权限的
private val REQUEST_CODE = 101
private fun requestCameraPermission() {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
return
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), REQUEST_CODE)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}else{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
}else{
}
}
}
}
然后要在xml文件里面添加一个 SurfaceView 样式可以自己随意配置。
private fun openCamera() {
if (!surfaceCreated) {
return
}
var cameraManager: CameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
val cameraIdList = cameraManager.cameraIdList
if (cameraIdList.isNotEmpty()) {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
return
}
val cameraId = cameraIdList[0]
cameraManager.openCamera(cameraId, object : CameraDevice.StateCallback() {
@RequiresApi(Build.VERSION_CODES.P)
override fun onOpened(camera: CameraDevice) {
Log.d(TAG, "onOpened")
mCamera = camera
camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
.addTarget(surfaceView.holder.surface)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
var surfaceList: MutableList<OutputConfiguration> = ArrayList()
var outputConfiguration =
OutputConfiguration(surfaceView.holder.surface)
surfaceList.add(outputConfiguration)
var session = SessionConfiguration(
SessionConfiguration.SESSION_REGULAR,
surfaceList, {
Log.d(TAG, "execute")
it.run()
}, callback
)
camera.createCaptureSession(session)
} else {
var surfaceList: MutableList<Surface> = ArrayList()
surfaceList.add(surfaceView.holder.surface)
camera.createCaptureSession(
surfaceList,
callback,
handler
)
}
}
override fun onDisconnected(camera: CameraDevice) {
Log.d(TAG, "onDisconnected")
}
override fun onError(camera: CameraDevice, error: Int) {
Log.d(TAG, "onError")
}
}, handler)
}
}
var callback = object : CameraCaptureSession.StateCallback() {
override fun onConfigured(captureSession: CameraCaptureSession) {
Log.d(TAG, "StateCallback.onConfigured")
val createCaptureRequest =
mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
createCaptureRequest.addTarget(surfaceView.holder.surface)
captureSession.setRepeatingRequest(
createCaptureRequest.build(),
object : CameraCaptureSession.CaptureCallback() {
override fun onCaptureCompleted(
session: CameraCaptureSession,
request: CaptureRequest,
result: TotalCaptureResult
) {
}
}, handler
)
}
override fun onConfigureFailed(session: CameraCaptureSession) {
Log.d(TAG, "StateCallback.onConfigureFailed")
}
}
private var handler: Handler = Handler(Looper.myLooper()!!) {
Log.d(TAG, "handleMessage")
false
}
这篇文章主要是写摄像头预览的简单的使用。
|