##問題
Camera2 APIを使用してカメラを使用したいです。
とりあえずカメラのプレビューをTextureViewに表示させることはできたのですが、プレビューが歪んで表示されてしまいます。
具体的にいますと、縦画面時は上下に伸びたような形となり、横画面時は横に伸びたように表示されます。
これをどうにかして正常に表示できるようにしたいです。
##試したこと
端末が対応している撮影サイズを取得して最大サイズをプレビューサイズとすることでうまくいくとの情報がありましたので、撮影サイズのリストを取得し、その最大サイズ(配列の要素0個目)をsetDefaultBufferSizeしてみましたがうまくいきませんでした。
お知恵をお貸しください。
##コード
DriveRecorderActivity.kt
Kotlin
1package caios.android.drive_recorder 2 3import android.content.Context 4import android.content.Intent 5import android.content.pm.PackageManager 6import android.graphics.* 7import android.hardware.camera2.CameraCaptureSession 8import android.hardware.camera2.CameraCharacteristics 9import android.hardware.camera2.CameraDevice 10import android.hardware.camera2.CameraManager 11import android.net.Uri 12import android.os.Build 13import androidx.appcompat.app.AppCompatActivity 14import android.os.Bundle 15import android.provider.Settings 16import android.util.Log 17import android.util.Size 18import android.view.Surface 19import android.view.TextureView 20import android.view.WindowManager 21import androidx.core.app.ActivityCompat 22import androidx.core.content.ContextCompat 23import kotlinx.android.synthetic.main.activity_drive_recorder.* 24import java.lang.Exception 25import java.util.jar.Manifest 26 27class DriveRecorderActivity : AppCompatActivity() { 28 29 private lateinit var textureView: TextureView 30 31 private var permissionRequestCount = 0 32 private var cameraDevice: CameraDevice? = null 33 private var cameraSize = Size(1920, 1080) 34 private var cameraCaptureSession: CameraCaptureSession? = null 35 36 private val cameraManager: CameraManager by lazy { 37 getSystemService(Context.CAMERA_SERVICE) as CameraManager 38 } 39 private val cameraId: String by lazy { 40 cameraManager.cameraIdList[0] 41 } 42 43 override fun onCreate(savedInstanceState: Bundle?) { 44 super.onCreate(savedInstanceState) 45 setContentView(R.layout.activity_drive_recorder) 46 47 textureView = ADR_TextureView 48 configurationTransform() 49 } 50 51 override fun onResume() { 52 super.onResume() 53 54 if (textureView.isAvailable) { 55 checkPermissionAndOpenCamera() 56 } 57 else { 58 textureView.surfaceTextureListener = object : TextureView.SurfaceTextureListener { 59 override fun onSurfaceTextureAvailable(p0: SurfaceTexture?, p1: Int, p2: Int) { 60 checkPermissionAndOpenCamera() 61 } 62 63 override fun onSurfaceTextureDestroyed(p0: SurfaceTexture?): Boolean { 64 return true 65 } 66 67 override fun onSurfaceTextureUpdated(p0: SurfaceTexture?) { 68 69 } 70 71 override fun onSurfaceTextureSizeChanged(p0: SurfaceTexture?, p1: Int, p2: Int) { 72 configurationTransform() 73 } 74 } 75 } 76 } 77 78 override fun onPause() { 79 super.onPause() 80 closeCamera() 81 } 82 83 private fun openCamera() { 84 if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) return 85 86 setCameraSize() 87 88 val cameraStateCallback = object : CameraDevice.StateCallback() { 89 override fun onOpened(p0: CameraDevice) { 90 cameraDevice = p0 91 createCameraPreview() 92 } 93 94 override fun onDisconnected(p0: CameraDevice) { 95 cameraDevice?.close() 96 cameraDevice = null 97 } 98 99 override fun onError(p0: CameraDevice, p1: Int) { 100 cameraDevice?.close() 101 cameraDevice = null 102 } 103 } 104 105 cameraManager.openCamera(cameraId, cameraStateCallback, handler) 106 } 107 108 private fun closeCamera() { 109 cameraCaptureSession?.close() 110 cameraDevice?.close() 111 } 112 113 private fun createCameraPreview(){ 114 try { 115 val surfaceTexture = textureView.surfaceTexture 116 surfaceTexture.setDefaultBufferSize(cameraSize.width, cameraSize.height) 117 118 val surface = Surface(surfaceTexture) 119 val previewRequestBuilder = cameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW) 120 previewRequestBuilder.addTarget(surface) 121 122 val captureSessionStateCallback = object : CameraCaptureSession.StateCallback() { 123 override fun onConfigured(p0: CameraCaptureSession) { 124 cameraCaptureSession = p0 125 cameraCaptureSession?.setRepeatingRequest(previewRequestBuilder.build(), null, null) 126 } 127 128 override fun onConfigureFailed(p0: CameraCaptureSession) { 129 130 } 131 } 132 133 cameraDevice?.createCaptureSession(listOf(surface), captureSessionStateCallback, null) 134 135 configurationTransform() 136 } 137 catch (e: Exception){ 138 Log.d(G_TAG, "$e") 139 } 140 } 141 142 private fun setCameraSize() { 143 val characteristic = cameraManager.getCameraCharacteristics(cameraId) 144 val scm = characteristic.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) 145 val mCameraSize = scm?.getOutputSizes(SurfaceTexture::class.java) ?: return 146 147 for(cameraSize in mCameraSize){ 148 Log.d(G_TAG, cameraSize.toString()) 149 } 150 151 cameraSize = mCameraSize[0] 152 153 configurationTransform() 154 } 155 156 private fun configurationTransform() { 157 val matrix = Matrix() 158 val centerX = textureView.width / 2f 159 val centerY = textureView.height / 2f 160 val orientation = getTextureViewOrientation() ?: return 161 162 matrix.postRotate(-orientation.toFloat(), centerX, centerY) 163 164 textureView.setTransform(matrix) 165 } 166 167 private fun getTextureViewOrientation(): Int?{ 168 try { 169 return when (textureView.display.rotation) { 170 Surface.ROTATION_0 -> 0 171 Surface.ROTATION_90 -> 90 172 Surface.ROTATION_180 -> 180 173 Surface.ROTATION_270 -> 270 174 else -> null 175 } 176 } 177 catch (e: Exception){ 178 Log.d(G_TAG, "$e") 179 return null 180 } 181 } 182 183 private fun checkPermissionAndOpenCamera() { 184 if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 185 if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.CAMERA) || permissionRequestCount == 0) { 186 ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA), 92) 187 permissionRequestCount++ 188 } 189 else { 190 val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", packageName, null)) 191 startActivity(intent) 192 } 193 } 194 else { 195 openCamera() 196 } 197 } 198 199 override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { 200 when(requestCode){ 201 92 -> { 202 if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){ 203 openCamera() 204 } 205 else { 206 global.toast(this, "権限不足") 207 } 208 } 209 } 210 } 211}
activity_drive_recorder.xml
XML
1<?xml version="1.0" encoding="utf-8"?> 2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:id="@+id/ADR_RootView" 8 tools:context=".DriveRecorderActivity"> 9 10 <TextureView 11 android:id="@+id/ADR_TextureView" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" /> 14 15</FrameLayout>

あなたの回答
tips
プレビュー