camera2 implementation (WIP)

This commit is contained in:
Captain Arepa 2024-05-21 16:05:23 -04:00
parent 990e071c65
commit c34e197bf3
2 changed files with 17 additions and 43 deletions

View file

@ -1,6 +1,6 @@
package com.example.cameraxtestappjava; package com.example.cameraxtestappjava;
import static com.example.cameraxtestappjava.camera.SegPassCamera.REQUEST_CAMERA_PERMISSION; import static com.example.cameraxtestappjava.camera.SegpassCamera.REQUEST_CAMERA_PERMISSION;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
@ -21,30 +21,18 @@ import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
import com.example.cameraxtestappjava.camera.CustomCamera; import com.example.cameraxtestappjava.camera.CustomCamera;
import com.example.cameraxtestappjava.camera.SegPassCamera; import com.example.cameraxtestappjava.camera.SegpassCamera;
import com.example.cameraxtestappjava.camera.CustomCameraCallback; import com.example.cameraxtestappjava.camera.CustomCameraCallback;
import com.example.cameraxtestappjava.databinding.ActivityMainBinding; import com.example.cameraxtestappjava.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
// binding
ActivityMainBinding binding; ActivityMainBinding binding;
CustomCamera mCamera;
// Camera2 // Camera2
private static final String TAG = "AndroidCameraApi"; private static final String TAG = "MainActivity";
private ImageButton mTakePictureButton; SegpassCamera mSegpassCamera;
private TextureView mTextureView;
private TextView tv;
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
SegPassCamera mCustomCamera2;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -63,32 +51,18 @@ public class MainActivity extends AppCompatActivity {
}); });
} }
private void setListeners() {
binding.cvCustomCamera.btnTakePicture.setOnClickListener(v -> mCamera.capturePhoto(new CustomCameraCallback() {
@Override
public void onPictureTakenSuccess(String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
@Override
public void onPictureTakenFailError(String error) {
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
}
}));
}
// Camera 2 // Camera 2
private void setUpCamera2() { private void setUpCamera2() {
// Initialize visual elements in host app // Initialize visual elements in host app
mTextureView = binding.c2Camera.tvCameraTextureView; TextureView mTextureView = binding.c2Camera.tvCameraTextureView;
mTakePictureButton = binding.c2Camera.btnTakepicture; ImageButton mTakePictureButton = binding.c2Camera.btnTakepicture;
// Initialized CustomCamera2 in host app // Initialized CustomCamera2 in host app
mCustomCamera2 = new SegPassCamera(this, mTextureView); mSegpassCamera = new SegpassCamera(this, mTextureView);
mCustomCamera2.init(); // Do not skip this!!! mSegpassCamera.init(); // Do not skip this!!!
mTakePictureButton.setOnClickListener(v -> mCustomCamera2.takePicture(new CustomCameraCallback() { mTakePictureButton.setOnClickListener(v -> mSegpassCamera.takePicture(new CustomCameraCallback() {
@Override @Override
public void onPictureTakenSuccess(String message) { public void onPictureTakenSuccess(String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
@ -118,20 +92,20 @@ public class MainActivity extends AppCompatActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Log.d(TAG, "onResume"); Log.d(TAG, "onResume");
mCustomCamera2.resumeCamera(); mSegpassCamera.resumeCamera();
} }
@Override @Override
protected void onPause() { protected void onPause() {
Log.d(TAG, "onPause"); Log.d(TAG, "onPause");
mCustomCamera2.pauseCamera(); mSegpassCamera.pauseCamera();
super.onPause(); super.onPause();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
Log.d(TAG, "onPause"); Log.d(TAG, "onPause");
mCustomCamera2.destroyCamera(); mSegpassCamera.destroyCamera();
super.onDestroy(); super.onDestroy();
} }
} }

View file

@ -42,8 +42,8 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
public class SegPassCamera { public class SegpassCamera {
private static final String TAG = "CustomCamera2"; private static final String TAG = "SegpassCamera";
Context mContext; Context mContext;
AppCompatActivity mActivity; AppCompatActivity mActivity;
@ -83,12 +83,12 @@ public class SegPassCamera {
} }
} }
public SegPassCamera(Context context, TextureView textureView) { public SegpassCamera(Context context, TextureView textureView) {
mContext = context; mContext = context;
mTextureView = textureView; mTextureView = textureView;
} }
public SegPassCamera(AppCompatActivity activity, TextureView textureView) { public SegpassCamera(AppCompatActivity activity, TextureView textureView) {
mActivity = activity; mActivity = activity;
mTextureView = textureView; mTextureView = textureView;
} }