This commit is contained in:
Captain Arepa 2024-05-17 15:37:14 -04:00
parent a9761aee8c
commit e8a56ca589
3 changed files with 35 additions and 116 deletions

View file

@ -1,22 +1,11 @@
package com.example.cameraxtestappjava;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
@ -24,21 +13,11 @@ import androidx.core.view.WindowInsetsCompat;
import com.example.cameraxtestappjava.camera.CustomCamera;
import com.example.cameraxtestappjava.camera.CustomCameraCallback;
import com.example.cameraxtestappjava.databinding.ActivityMainBinding;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
ProcessCameraProvider cameraProvider;
ImageCapture imageCapture;
ImageCapture.OutputFileOptions outputFileOptions;
ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
ContentValues contentValues;
String fileName;
Uri tempFileUri;
ActivityMainBinding binding;
CustomCamera mCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -48,13 +27,10 @@ public class MainActivity extends AppCompatActivity {
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
CustomCamera camera = new CustomCamera(this, binding.pvViewfinder);
camera.setUpCamera();
mCamera = new CustomCamera(this, binding.cvCustomCamera.pvViewfinder);
mCamera.setUpCamera();
binding.btnTakePicture.setOnClickListener(v -> takePhoto(camera));
//setListeners();
//setUpCamera();
setListeners();
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
@ -63,8 +39,8 @@ public class MainActivity extends AppCompatActivity {
});
}
private void takePhoto(CustomCamera camera) {
camera.capturePhoto(new CustomCameraCallback() {
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();
@ -74,80 +50,6 @@ public class MainActivity extends AppCompatActivity {
public void onPictureTakenFailError(String error) {
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
}
});
}
private void setListeners() {
binding.btnTakePicture.setOnClickListener(v -> capturePhoto());
}
private void capturePhoto() {
if (imageCapture == null) return;
fileName = System.currentTimeMillis() + "";
outputFileOptions = getOutputFileOptions(fileName);
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
tempFileUri = outputFileResults.getSavedUri();
Toast.makeText(MainActivity.this, "Image saved! " + tempFileUri, Toast.LENGTH_LONG).show();
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
Toast.makeText(MainActivity.this, "Image Not Saved " + exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@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");
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraXTestApp");
}
return new ImageCapture.OutputFileOptions.Builder(
getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues
).build();
}
private void setUpCamera() {
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
startCustomCamera(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}, ContextCompat.getMainExecutor(this)); // por parametro
}
private void startCustomCamera(ProcessCameraProvider cameraProvider) {
Log.d("TAGTAG", "UEEEP");
CameraSelector cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA;
Preview preview = new Preview.Builder().build();
preview.setSurfaceProvider(binding.pvViewfinder.getSurfaceProvider());
imageCapture = new ImageCapture.Builder().build();
try {
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture);
} catch (Exception e) {
e.printStackTrace();
}
}));
}
}

View file

@ -7,7 +7,13 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.camera.view.PreviewView
<include
android:id="@+id/cvCustomCamera"
layout="@layout/custom_camera_view"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<!--<androidx.camera.view.PreviewView
android:id="@+id/pvViewfinder"
android:layout_width="match_parent"
android:layout_height="0dp"
@ -34,6 +40,6 @@
android:layout_height="wrap_content"
android:text="Take Picture" />
</LinearLayout>
</LinearLayout>-->
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -1,13 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.camera.view.PreviewView 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"
android:id="@+id/pvCameraViewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/pvViewfinder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@id/llCameraButtons"
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"
@ -15,12 +20,18 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="horizontal">
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/pvViewfinder">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnTakePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture" />
</LinearLayout>
</androidx.camera.view.PreviewView>
</FrameLayout>