control commit

This commit is contained in:
Captain Arepa 2024-05-17 11:28:30 -04:00
parent 7604a00532
commit fed45c298b
3 changed files with 80 additions and 59 deletions

View file

@ -3,6 +3,7 @@ package com.example.cameraxtestappjava;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
@ -43,7 +44,9 @@ public class MainActivity extends AppCompatActivity {
customCamera = new CustomCamera(this);
customCamera.initCustomCamera(this, ContextCompat.getMainExecutor(this), this);
binding.scSegpassCameraView.takePictureBtn.setOnClickListener(v -> takePicture());
Log.d("TAGTAG", "UEEEPA");
//binding.scSegpassCameraView.takePictureBtn.setOnClickListener(v -> takePicture());
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());

View file

@ -2,11 +2,13 @@ package com.example.cameraxtestappjava.camera;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.FrameLayout;
@ -27,7 +29,9 @@ import com.google.common.util.concurrent.ListenableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
public class CustomCamera extends FrameLayout {
public class CustomCamera extends FrameLayout implements TextureView.SurfaceTextureListener {
private final String TAG = this.getClass().getSimpleName();
// View
private final View rootView;
@ -40,23 +44,22 @@ public class CustomCamera extends FrameLayout {
private LifecycleOwner mLifecycleOwner;
// Camera
private ProcessCameraProvider cameraProvider;
private ImageCapture imageCapture;
private ImageCapture.OutputFileOptions outputFileOptions;
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
private ContentValues contentValues;
private String fileName;
private Uri tempFileUri;
private ProcessCameraProvider mCameraProvider;
private ImageCapture mImageCapture;
private ListenableFuture<ProcessCameraProvider> mCameraProviderFuture;
private Uri mTempFileUri;
public CustomCamera(@NonNull Context context) {
super(context);
rootView = inflate(context, R.layout.custom_camera_view, this);
findIds();
}
public CustomCamera(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
// Aqui van los estilos y todo eso si se requiere
rootView = inflate(context, R.layout.custom_camera_view, this);
findIds();
}
public void initCustomCamera(@NonNull Context context, Executor executor, LifecycleOwner lifecycleOwner) {
@ -67,13 +70,12 @@ public class CustomCamera extends FrameLayout {
}
private void initComponents() {
findIds();
initializeCamera();
initializeListeners();
}
private void findIds() {
cameraViewFinder = rootView.findViewById(R.id.pvViewfinder);
cameraViewFinder = rootView.findViewById(R.id.pvCameraViewFinder);
takePictureBtn = rootView.findViewById(R.id.btnTakePicture);
}
@ -83,17 +85,18 @@ public class CustomCamera extends FrameLayout {
// El callback se tiene que implementar del lado de la aplicacion integradora
public void capturePhoto(CustomCameraCallback callback) {
if (imageCapture == null) return;
if (mImageCapture == null) return;
fileName = System.currentTimeMillis() + "";
String mFileName = System.currentTimeMillis() + "";
outputFileOptions = getOutputFileOptions(fileName);
ImageCapture.OutputFileOptions mIutputFileOptions = getOutputFileOptions(mFileName);
imageCapture.takePicture(outputFileOptions, mExecutor, new ImageCapture.OnImageSavedCallback() {
try {
mImageCapture.takePicture(mIutputFileOptions, mExecutor, new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
tempFileUri = outputFileResults.getSavedUri();
callback.onPictureTakenSuccess("Image saved! " + tempFileUri);
mTempFileUri = outputFileResults.getSavedUri();
callback.onPictureTakenSuccess("Image saved! " + mTempFileUri);
//Toast.makeText(mContext, "Image saved! " + tempFileUri, Toast.LENGTH_LONG).show();
}
@ -103,31 +106,34 @@ public class CustomCamera extends FrameLayout {
//Toast.makeText(mContext, "Image Not Saved " + exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
callback.onPictureTakenFailError(e.getMessage());
}
}
@NonNull
private ImageCapture.OutputFileOptions getOutputFileOptions(String name) {
contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
ContentValues mContentValues = new ContentValues();
mContentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
mContentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraXTestApp");
mContentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraXTestApp");
}
return new ImageCapture.OutputFileOptions.Builder(
mContext.getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues
mContext.getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mContentValues
).build();
}
private void initializeCamera() {
cameraProviderFuture = ProcessCameraProvider.getInstance(mContext);
cameraProviderFuture.addListener(() -> {
mCameraProviderFuture = ProcessCameraProvider.getInstance(mContext);
mCameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
startCustomCamera(cameraProvider);
mCameraProvider = mCameraProviderFuture.get();
startCustomCamera(mCameraProvider);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
@ -142,15 +148,37 @@ public class CustomCamera extends FrameLayout {
Preview preview = new Preview.Builder().build();
preview.setSurfaceProvider(cameraViewFinder.getSurfaceProvider());
imageCapture = new ImageCapture.Builder().build();
mImageCapture = new ImageCapture.Builder().build();
try {
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(mLifecycleOwner, cameraSelector, preview, imageCapture);
cameraProvider.bindToLifecycle(mLifecycleOwner, cameraSelector, preview, mImageCapture);
} catch (Exception e) {
e.printStackTrace();
}
}
// Surface Texture Listeners
@Override
public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
startCustomCamera(mCameraProvider);
}
@Override
public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
Log.d(TAG, "STUFF");
}
@Override
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
Log.d(TAG, "MORE STUFF");
}
}

View file

@ -1,36 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.camera.view.PreviewView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/pvCameraViewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.camera.view.PreviewView
android:id="@+id/pvViewfinder"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/llCameraButtons"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/llCameraButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/pvViewfinder">
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnTakePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.camera.view.PreviewView>