実現したいこと
camera2 api を使用し、ズーム機能を搭載したカメラアプリを作成したい
発生している問題・分からないこと
oncreateの
mMaxZoom =characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);からmCropRegion = activeRect; の範囲で不明なクラスというエラー文が出ます。どなたかご教授お願い致します。
該当のソースコード
package com.vuzix.sample.video_encoder; import android.Manifest; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.graphics.ImageFormat; import android.graphics.Rect; import android.graphics.SurfaceTexture; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CaptureRequest; import android.media.Image; import android.media.ImageReader; import android.media.MediaCodec; import android.media.MediaCodecInfo; import android.media.MediaCodecList; import android.media.MediaFormat; import android.media.MediaMuxer; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.HandlerThread; import android.util.Log; import android.view.Surface; import android.view.TextureView; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Objects; public class MainActivity extends Activity implements RotationListener.rotationCallbackFn{ public static final String TAG = "MediaCodec_App"; private Button mRecordButton; private TextureView mTextureView; private String mCameraId; protected CameraDevice mCameraDevice; protected CameraCaptureSession mCameraCaptureSessions; protected CaptureRequest.Builder mCaptureRequestBuilder; private ImageReader mImageReader; private RotationListener mRotationListener; private Button wordButton; private boolean mRecognizerActive = false; public final String CUSTOM_SDK_INTENT = "com.vuzix.sample.vuzix_voicecontrolwithsdk.CustomIntent"; private Handler mBackgroundHandler; private HandlerThread mBackgroundThread; private Handler mBackgroundPreviewHandler; private HandlerThread mBackgroundPreviewThread; private Handler mBackgroundCodecHandler; private HandlerThread mBackgroundCodecThread; private static final int REQUEST_CAMERA_PERMISSION = 200; // parameters for the encoder private static final int FRAME_RATE = 24; // 24fps private static final int IFRAME_INTERVAL = 5; // 5 seconds between I-frames private static final int MAX_QUEUE_ELEMENTS = 8; private static final String MIME_TYPE = "video/avc"; // H.264 Advanced Video Coding private static final String ENCODER = "OMX.qcom.video.encoder.avc"; // HW encoder //private static final String ENCODER = "OMX.google.h264.encoder"; // SW encoder private MediaCodec mEncoder; private MediaMuxer mMuxer; private MediaFormat mEncoderFormat = null; private LinkedList<byte[]> mBytesQueue; private final Object mBytesQueueLock = new Object(); private int mQueueElementCount; private boolean mVideoRecording = false; private boolean mMuxerStarted; private boolean mCaptureSessionStopped; private int mTrackIndex; private long mFramesIndex; VoiceCmdReceiver mVoiceCmdReceiver; private MyIntentReceiver myIntentReceiver; private View recordButton; // ズーム関連のフィールド private float mZoomLevel = 1.0f; // 現在のズームレベル private float mMaxZoom = 1.0f; // カメラがサポートする最大ズーム private Rect mCropRegion; // ズームの適用範囲 public MainActivity(CameraManager manager) throws CameraAccessException { this.manager = manager; } /** * Setup the view when created * @param savedInstanceState - unused, passed to superclass */ @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); } private CameraManager manager; CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId); mMaxZoom = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); // カメラセンサーのサイズを取得 Rect activeRect = characteristics.get(CameraCharacteristics.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(); assert texture != null; texture.setDefaultBufferSize(1920, 1000);// preview size 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 (null == mCameraDevice) { return; } mCameraCaptureSessions = session; updatePreview(); } @Override public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { 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]; // カメラIDを取得 CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId); // 最大ズーム倍率を取得 mMaxZoom = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); Log.d(TAG, "Max Zoom: " + mMaxZoom); // センサーのアクティブ領域を初期化 mCropRegion = characteristics.get(CameraCharacteristics.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; }
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
定義や初期化するコードの呼び出しなど試しましたが不明なクラスとなってしまいます
補足
特になし
- Androidの質問であれば、それ相応のタグを追加してください。
- エラー文は勝手に解釈せずに、コピペで記載してください。
>oncreateの
>mMaxZoom =characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);からmCropRegion = activeRect; の範囲で不明なクラスというエラー文
onCreate の外にあるように見えますが?
コメントありがとうございます。⚪︎⚪︎を試したところ問題が解決しました!
ベストアンサーに選びたいので同じ内容を回答欄に投稿いただけますでしょうか?
回答1件
あなたの回答
tips
プレビュー