camera2 implementation (WIP)

This commit is contained in:
Captain Arepa 2024-05-21 16:01:49 -04:00
parent 467f07ccdc
commit 990e071c65
2 changed files with 17 additions and 53 deletions

View file

@ -1,30 +1,10 @@
package com.example.cameraxtestappjava;
import static com.example.cameraxtestappjava.camera.CustomCamera2.REQUEST_CAMERA_PERMISSION;
import static com.example.cameraxtestappjava.camera.SegPassCamera.REQUEST_CAMERA_PERMISSION;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
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.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
@ -36,29 +16,15 @@ import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.cameraxtestappjava.camera.CustomCamera;
import com.example.cameraxtestappjava.camera.CustomCamera2;
import com.example.cameraxtestappjava.camera.SegPassCamera;
import com.example.cameraxtestappjava.camera.CustomCameraCallback;
import com.example.cameraxtestappjava.databinding.ActivityMainBinding;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@ -78,7 +44,7 @@ public class MainActivity extends AppCompatActivity {
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
CustomCamera2 mCustomCamera2;
SegPassCamera mCustomCamera2;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -113,9 +79,15 @@ public class MainActivity extends AppCompatActivity {
// Camera 2
private void setUpCamera2() {
// Initialize visual elements in host app
mTextureView = binding.c2Camera.tvCameraTextureView;
mTakePictureButton = binding.c2Camera.btnTakepicture;
mCustomCamera2 = new CustomCamera2(this, mTextureView);
// Initialized CustomCamera2 in host app
mCustomCamera2 = new SegPassCamera(this, mTextureView);
mCustomCamera2.init(); // Do not skip this!!!
mTakePictureButton.setOnClickListener(v -> mCustomCamera2.takePicture(new CustomCameraCallback() {
@Override
public void onPictureTakenSuccess(String message) {

View file

@ -31,7 +31,6 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@ -43,7 +42,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomCamera2 {
public class SegPassCamera {
private static final String TAG = "CustomCamera2";
Context mContext;
@ -84,31 +83,22 @@ public class CustomCamera2 {
}
}
public CustomCamera2(Context context, TextureView textureView) {
public SegPassCamera(Context context, TextureView textureView) {
mContext = context;
mTextureView = textureView;
}
public CustomCamera2(AppCompatActivity activity, TextureView textureView) {
public SegPassCamera(AppCompatActivity activity, TextureView textureView) {
mActivity = activity;
mTextureView = textureView;
}
private void setUpCamera2() {
public void init() {
createFolder();
setUpListeners();
/*tv = findViewById(R.id.tv);
mTakePictureButton = binding.c2Camera.btnTakepicture;
mTakePictureButton.setOnClickListener(v -> {
takePicture();
});*/
}
public void setUpListeners() {
private void setUpListeners() {
mTextureView.setSurfaceTextureListener(textureListener);
}
@ -257,6 +247,8 @@ public class CustomCamera2 {
mCameraManager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}