From e8a56ca589196b1d7df69cb90335cc6a12930f1b Mon Sep 17 00:00:00 2001 From: Captain Arepa Date: Fri, 17 May 2024 15:37:14 -0400 Subject: [PATCH] stuff --- .../cameraxtestappjava/MainActivity.java | 114 ++---------------- app/src/main/res/layout/activity_main.xml | 10 +- .../main/res/layout/custom_camera_view.xml | 27 +++-- 3 files changed, 35 insertions(+), 116 deletions(-) diff --git a/app/src/main/java/com/example/cameraxtestappjava/MainActivity.java b/app/src/main/java/com/example/cameraxtestappjava/MainActivity.java index 18fd885..d7d4cf8 100644 --- a/app/src/main/java/com/example/cameraxtestappjava/MainActivity.java +++ b/app/src/main/java/com/example/cameraxtestappjava/MainActivity.java @@ -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 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(); - } - + })); } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8350491..4d773fc 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,7 +7,13 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + + \ No newline at end of file diff --git a/app/src/main/res/layout/custom_camera_view.xml b/app/src/main/res/layout/custom_camera_view.xml index 3464672..80533b6 100644 --- a/app/src/main/res/layout/custom_camera_view.xml +++ b/app/src/main/res/layout/custom_camera_view.xml @@ -1,13 +1,18 @@ - + android:layout_height="match_parent"> + + + android:orientation="horizontal" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/pvViewfinder"> + + - +