#使用条件等
使用端末:SONY xperiaZ5(Android7.0)カメラは最大60fps
openCV3.4.3
使用API:camera2
AndroidStudio3.1.3
##困っていること
Androidでカメラからの映像をopenCVを用いて色抽出処理を必要としています。
当初はopenCVによる入力方法を用いていましたがopenCVによる入力は低速で、フレームレートが低かったため(25fps程度)camera2APIを用いようとしています。最低でも安定して30fpsは必要としています。
以下のサンプルコードでcamera2によってプレビューするだけのプログラムで高フレームレート(openCV比で。数値は未確認)でのプレビューが可能であることが確認できています。
openCVの基本形式であるMAT形式にはbitmap(BitmapToMatメソッド)が使用できることがわかりました。
一方、サンプルプログラムで使用しているTextureViewから(getBitmapメソッド)を用いてbitmapが取得できるということを知りました。
これらを用いることでopenCVに画像データを渡せると考えていますが、サンプルコードのどの部分にどのような処理を書けばよいか理解ができていません。
上記を可能とする方法、あるいは別の解決法など、ご助言いただければ幸いです。
MainActivity.java
lang
1 2import android.app.Activity; 3import android.content.Context; 4import android.graphics.SurfaceTexture; 5import android.hardware.camera2.CameraAccessException; 6import android.hardware.camera2.CameraCaptureSession; 7import android.hardware.camera2.CameraCharacteristics; 8import android.hardware.camera2.CameraDevice; 9import android.hardware.camera2.CameraManager; 10import android.hardware.camera2.CaptureRequest; 11import android.hardware.camera2.params.StreamConfigurationMap; 12import android.os.Build; 13import android.os.Bundle; 14import android.os.Handler; 15import android.os.HandlerThread; 16import android.support.annotation.NonNull; 17import android.util.Size; 18import android.view.Surface; 19import android.view.TextureView; 20import android.view.View; 21import android.view.WindowManager; 22import android.widget.Toast; 23 24import java.util.Collections; 25 26public class MainActivity extends Activity { 27 private Camera mCamera; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 if (Build.VERSION.SDK_INT >= 19) { 33 View decor = this.getWindow().getDecorView(); 34 decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 35 } else { 36 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 37 } 38 setContentView(R.layout.activity_main); 39 40 TextureView textureView = (TextureView) findViewById(R.id.texture_view); 41 textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { 42 @Override 43 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 44 mCamera.open(); 45 } 46 47 @Override 48 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {} 49 50 @Override 51 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 52 return false; 53 } 54 55 @Override 56 public void onSurfaceTextureUpdated(SurfaceTexture surface) {} 57 }); 58 59 mCamera = new Camera(textureView); 60 } 61 @Override 62 protected void onResume() {//追記 63 super.onResume(); 64 if (Build.VERSION.SDK_INT >= 19) { 65 View decor = this.getWindow().getDecorView(); 66 decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 67 } else { 68 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 69 } 70 } 71 72 class Camera { 73 private CameraDevice mCamera; 74 private TextureView mTextureView; 75 private Size mCameraSize; 76 private CaptureRequest.Builder mPreviewBuilder; 77 private CameraCaptureSession mPreviewSession; 78 79 private CameraDevice.StateCallback mCameraDeviceCallback = new CameraDevice.StateCallback() { 80 @Override 81 public void onOpened(@NonNull CameraDevice camera) { 82 mCamera = camera; 83 createCaptureSession(); 84 } 85 86 @Override 87 public void onDisconnected(@NonNull CameraDevice camera) { 88 camera.close(); 89 mCamera = null; 90 } 91 92 @Override 93 public void onError(@NonNull CameraDevice camera, int error) { 94 camera.close(); 95 mCamera = null; 96 } 97 }; 98 99 CameraCaptureSession.StateCallback mCameraCaptureSessionCallback = new CameraCaptureSession.StateCallback() { 100 @Override 101 public void onConfigured(@NonNull CameraCaptureSession session) { 102 mPreviewSession = session; 103 updatePreview(); 104 } 105 106 @Override 107 public void onConfigureFailed(@NonNull CameraCaptureSession session) { 108 Toast.makeText(MainActivity.this, "onConfigureFailed", Toast.LENGTH_LONG).show(); 109 } 110 }; 111 112 public Camera(TextureView textureView) { 113 mTextureView = textureView; 114 } 115 116 public void open() { 117 try { 118 CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 119 for (String cameraId : manager.getCameraIdList()) { 120 CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); 121 if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) { 122 StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 123 mCameraSize = map.getOutputSizes(SurfaceTexture.class)[4]; 124 manager.openCamera(cameraId, mCameraDeviceCallback, null); 125 126 return; 127 } 128 } 129 } catch (CameraAccessException e) { 130 e.printStackTrace(); 131 } 132 } 133 134 private void createCaptureSession() { 135 if (!mTextureView.isAvailable()) { 136 return; 137 } 138 139 SurfaceTexture texture = mTextureView.getSurfaceTexture(); 140 texture.setDefaultBufferSize(mCameraSize.getWidth(), mCameraSize.getHeight()); 141 Surface surface = new Surface(texture); 142 try { 143 mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 144 } catch (CameraAccessException e) { 145 e.printStackTrace(); 146 } 147 148 mPreviewBuilder.addTarget(surface); 149 try { 150 mCamera.createCaptureSession(Collections.singletonList(surface), mCameraCaptureSessionCallback, null); 151 } catch (CameraAccessException e) { 152 e.printStackTrace(); 153 } 154 } 155 156 private void updatePreview() { 157 mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 158 HandlerThread thread = new HandlerThread("CameraPreview"); 159 thread.start(); 160 Handler backgroundHandler = new Handler(thread.getLooper()); 161 162 try { 163 mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, backgroundHandler); 164 } catch (CameraAccessException e) { 165 e.printStackTrace(); 166 } 167 } 168 } 169}
AndroidManifest.xml
lang
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.kobayashi.camera2_highspeed_sumple"> 4 <uses-permission android:name="android.permission.CAMERA"/> 5 6 <application 7 android:allowBackup="true" 8 android:icon="@mipmap/ic_launcher" 9 android:label="@string/app_name" 10 android:roundIcon="@mipmap/ic_launcher_round" 11 android:supportsRtl="true" 12 android:theme="@style/AppTheme"> 13 <activity android:name=".MainActivity"> 14 <intent-filter> 15 <action android:name="android.intent.action.MAIN" /> 16 17 <category android:name="android.intent.category.LAUNCHER" /> 18 </intent-filter> 19 </activity> 20 </application> 21 22</manifest>
activity_main.xml
lang
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout 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 tools:context=".MainActivity"> 8 9 <FrameLayout 10 xmlns:android="http://schemas.android.com/apk/res/android" 11 xmlns:tools="http://schemas.android.com/tools" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 tools:context="ueder.cameratest.MainActivity"> 15 16 <TextureView 17 android:id="@+id/texture_view" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent" /> 20 </FrameLayout> 21 22 23</android.support.constraint.ConstraintLayout>
あなたの回答
tips
プレビュー