This commit is contained in:
Captain Arepa 2024-05-23 15:12:26 -04:00
parent 43415be913
commit 6fdb9f4f7b
4 changed files with 96 additions and 42 deletions

View file

@ -1,7 +1,9 @@
package com.example.cameraxtestappjava;
import static com.example.cameraxtestappjava.segpasscamerra.SegpassNewCamera2.REQUEST_CAMERA_PERMISSION;
import static com.example.cameraxtestappjava.segpasscamerra.Camera2NewLib.REQUEST_CAMERA_PERMISSION;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
@ -14,6 +16,7 @@ import androidx.core.view.WindowInsetsCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.view.WindowManager;
import com.example.cameraxtestappjava.camera.AutoFitTextureView;
@ -21,23 +24,25 @@ import com.example.cameraxtestappjava.camera.SegpassCameraCallback;
import com.example.cameraxtestappjava.camera.SegpassCameraStateCallback;
import com.example.cameraxtestappjava.camera.SegpassPermissionListener;
import com.example.cameraxtestappjava.databinding.ActivityCameraNewBinding;
import com.example.cameraxtestappjava.segpasscamerra.SegpassNewCamera2;
import com.example.cameraxtestappjava.segpasscamerra.Camera2NewLib;
public class CameraActivityNew extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback, SegpassCameraCallback, SegpassCameraStateCallback, SegpassPermissionListener {
ActivityCameraNewBinding binding;
SegpassNewCamera2 mCamera2;
Camera2NewLib mCamera2;
AutoFitTextureView mTextureView;
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
binding = ActivityCameraNewBinding.inflate(getLayoutInflater());
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(binding.getRoot());
mTextureView = binding.inCamera2.tvCameraTextureView;
mCamera2 = new SegpassNewCamera2(this, mTextureView, this, this);
mCamera2 = new Camera2NewLib(this, mTextureView, this, this);
mCamera2.init();
setUpListeners();
@ -74,8 +79,7 @@ public class CameraActivityNew extends AppCompatActivity implements ActivityComp
private void setUpListeners() {
binding.inCamera2.btnTakepicture.setOnClickListener(v -> {
mCamera2.takePhoto();
mCamera2.showToast("eueep");
mCamera2.takePicture(this);
});
}

View file

@ -36,6 +36,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.example.cameraxtestappjava.camera.AutoFitTextureView;
import com.example.cameraxtestappjava.camera.SegpassCameraCallback;
import com.example.cameraxtestappjava.camera.SegpassCameraStateCallback;
import com.example.cameraxtestappjava.camera.SegpassPermissionListener;
@ -47,7 +48,7 @@ import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class SegpassNewCamera2 {
public class Camera2NewLib {
/**
* Tag for the {@link Log}.
@ -201,7 +202,7 @@ public class SegpassNewCamera2 {
* @param textureView
* @param listener
*/
public SegpassNewCamera2(AppCompatActivity activity, AutoFitTextureView textureView, SegpassPermissionListener listener) {
public Camera2NewLib(AppCompatActivity activity, AutoFitTextureView textureView, SegpassPermissionListener listener) {
mActivity = activity;
mTextureView = textureView;
mPermissionListener = listener;
@ -215,7 +216,7 @@ public class SegpassNewCamera2 {
* @param listener
* @param stateCallback
*/
public SegpassNewCamera2(AppCompatActivity activity, AutoFitTextureView textureView, SegpassPermissionListener listener, @Nullable SegpassCameraStateCallback stateCallback) {
public Camera2NewLib(AppCompatActivity activity, AutoFitTextureView textureView, SegpassPermissionListener listener, @Nullable SegpassCameraStateCallback stateCallback) {
mActivity = activity;
mTextureView = textureView;
mPermissionListener = listener;
@ -314,6 +315,7 @@ public class SegpassNewCamera2 {
private void process(CaptureResult result) {
Log.d(TAG, "STATE " + mState);
//captureStillPicture();
switch (mState) {
case STATE_PREVIEW: {
// We have nothing to do when the camera preview is working normally.
@ -598,16 +600,11 @@ public class SegpassNewCamera2 {
e.printStackTrace();
} catch (NullPointerException e) {
mCameraStateCallback.onCameraInitError(e.getMessage());
// Currently an NPE is thrown when the Camera2API is used but not supported on the
// device this code runs.
/*ErrorDialog.newInstance(getString(R.string.camera_error))
.show(getChildFragmentManager(), FRAGMENT_DIALOG);*/
/// callback here
}
}
/**
* Opens the camera specified by {@link SegpassNewCamera2#mCameraId}.
* Opens the camera specified by {@link Camera2NewLib#mCameraId}.
*/
private void openCamera(int width, int height) {
// Check permissions
@ -693,8 +690,9 @@ public class SegpassNewCamera2 {
// Auto focus should be continuous for camera preview.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// Flash is automatically enabled when necessary.
setAutoFlash(mPreviewRequestBuilder);
// Trigger AF
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_START);
// Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build();
@ -754,8 +752,76 @@ public class SegpassNewCamera2 {
/**
* Initiate a still image capture.
*/
public void takePhoto() {
lockFocus();
public void takePicture(SegpassCameraCallback cameraCallback) {
if (mCameraDevice == null) {
Log.e(TAG, "cameraDevice is null");
return;
}
try {
CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId);
if (characteristics == null)
throw new CameraAccessException(CameraAccessException.CAMERA_ERROR);
Size[] jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
if (jpegSizes == null) throw new NullPointerException("Error taking picture");
int width = 640;
int height = 480;
if (jpegSizes.length > 0) {
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}
ImageReader mReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
List<Surface> outputSurfaces = new ArrayList<Surface>(2);
outputSurfaces.add(mReader.getSurface());
outputSurfaces.add(new Surface(mTextureView.getSurfaceTexture()));
final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mReader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
// Orientation
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
String fileName = "IMG_" + System.currentTimeMillis() + ".jpg";
mFile = new File(mFileFolder + "/" + fileName); // Ver como hacer para guardar en la carpeta de la app
mReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Log.d(TAG, "file");
//Toast.makeText(mActivity, "Saved:" + mFile, Toast.LENGTH_SHORT).show();
Log.d(TAG, mFile.toString());
cameraCallback.onPictureTakenSuccess("Saved:" + mFile);
createCameraPreviewSession();
}
};
mCameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
} catch (CameraAccessException e) {
//showCameraStateError("onConfigured@takePicture(): " + e.getMessage());
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
cameraCallback.onPictureTakenFailError(e.getMessage());
}
}
/**
@ -811,11 +877,6 @@ public class SegpassNewCamera2 {
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mImageReader.getSurface());
// Use the same AE and AF modes as the preview.
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
setAutoFlash(captureBuilder);
// Orientation
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
@ -864,7 +925,6 @@ public class SegpassNewCamera2 {
// Reset the auto-focus trigger
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
setAutoFlash(mPreviewRequestBuilder);
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
mBackgroundHandler);
// After this, the camera will go back to the normal state of preview.
@ -876,13 +936,6 @@ public class SegpassNewCamera2 {
}
}
private void setAutoFlash(CaptureRequest.Builder requestBuilder) {
if (mFlashSupported) {
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
}
}
/**
* Create Folder
*/

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
@ -13,4 +13,4 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -9,21 +9,18 @@
<com.example.cameraxtestappjava.camera.AutoFitTextureView
android:id="@+id/tvCameraTextureView"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/btn_takepicture"
android:layout_alignParentTop="true" />
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/btn_takepicture"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="?android:selectableItemBackground"
android:contentDescription="take_photo"
android:padding="10dip"
android:layout_gravity="bottom|center"
android:scaleType="fitCenter"
android:src="@drawable/capture_button_video" />
</RelativeLayout>
</FrameLayout>