実現したいこと
作成した動画アプリにズーム機能を搭載したい
発生している問題・分からないこと
動画を撮ることはでき、そこにズーム機能を搭載したいと考え、やってみたところ多数のエラーが出てきてしまった
エラーメッセージ
error
1java.lang.RuntimeException: Unable to resume activity {com.vuzix.sample.vuzix_speech_recognition/com.vuzix.sample.video_encoder.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.TextureView.isAvailable()' on a null object reference 2 3Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.TextureView.isAvailable()' on a null object reference 4 at com.vuzix.sample.video_encoder.MainActivity.onResume(MainActivity.java:787)
該当のソースコード
private float mZoomLevel = 1.0f; private float mMaxZoom = 1.0f; private Rect mCropRegion; private CameraManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openCamera(); manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); mRecordButton = (Button) findViewById(R.id.btn_record); wordButton = (Button) findViewById(R.id.myButton); mRecordButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onRecordOrStopClick(); } }); myIntentReceiver = new MyIntentReceiver(); registerReceiver(myIntentReceiver , new IntentFilter(CUSTOM_SDK_INTENT), Context.RECEIVER_NOT_EXPORTED); mVoiceCmdReceiver = new VoiceCmdReceiver(this); CameraCharacteristics characteristics = null; try { characteristics = manager.getCameraCharacteristics(mCameraId); } catch (CameraAccessException e) { throw new RuntimeException(e); } mMaxZoom = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); Rect activeRect = characteristics.get(SENSOR_INFO_ACTIVE_ARRAY_SIZE); mCropRegion = activeRect; } private void setZoom(float zoomLevel) { if (zoomLevel < 1.0f || zoomLevel > mMaxZoom) { Log.e(TAG, "Invalid zoom level: " + zoomLevel); return; } mZoomLevel = zoomLevel; Rect activeRect = mCropRegion; int cropWidth = (int) (activeRect.width() / mZoomLevel); int cropHeight = (int) (activeRect.height() / mZoomLevel); int cropX = (activeRect.width() - cropWidth) / 2; int cropY = (activeRect.height() - cropHeight) / 2; mCropRegion = new Rect(cropX, cropY, cropX + cropWidth, cropY + cropHeight); mCaptureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, mCropRegion); try { mCameraCaptureSessions.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } protected void createCameraPreview() { try { SurfaceTexture texture = mTextureView.getSurfaceTexture(); if (texture == null) { throw new IllegalStateException("SurfaceTexture is not ready"); } texture.setDefaultBufferSize(1920, 1000); Surface surface = new Surface(texture); mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mCaptureRequestBuilder.addTarget(surface); mCameraDevice.createCaptureSession(Collections.singletonList(surface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { if (mCameraDevice == null) { return; } mCameraCaptureSessions = session; updatePreview(); } @Override public void onConfigureFailed(CameraCaptureSession session) { Toast.makeText(MainActivity.this, "Configuration change", Toast.LENGTH_SHORT).show(); } }, mBackgroundPreviewHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } private void openCamera() { CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { mCameraId = manager.getCameraIdList()[0]; CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId); mMaxZoom = characteristics.get(SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); Log.d(TAG, "Max Zoom: " + mMaxZoom); mCropRegion = characteristics.get(SENSOR_INFO_ACTIVE_ARRAY_SIZE); if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION); return; } manager.openCamera(mCameraId, new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice camera) { mCameraDevice = camera; createCameraPreview(); } @Override public void onDisconnected(CameraDevice camera) { camera.close(); mCameraDevice = null; } @Override public void onError(CameraDevice camera, int error) { camera.close(); mCameraDevice = null; } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override protected void onResume() { super.onResume(); startBackgroundThread(); if (mTextureView.isAvailable()) { createCameraPreview(); } else { mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { createCameraPreview(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }); } }
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
おそらく、createCameraPreview()、onResume の部分でエラーが起きていると考えているのですが、どのように間違っているかがわかりませんでした。
補足
特になし
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。