control commit
This commit is contained in:
parent
7604a00532
commit
fed45c298b
3 changed files with 80 additions and 59 deletions
|
@ -3,6 +3,7 @@ package com.example.cameraxtestappjava;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
|
@ -43,7 +44,9 @@ public class MainActivity extends AppCompatActivity {
|
||||||
customCamera = new CustomCamera(this);
|
customCamera = new CustomCamera(this);
|
||||||
customCamera.initCustomCamera(this, ContextCompat.getMainExecutor(this), 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) -> {
|
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
|
||||||
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
||||||
|
|
|
@ -2,11 +2,13 @@ package com.example.cameraxtestappjava.camera;
|
||||||
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.SurfaceTexture;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.TextureView;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
|
@ -27,7 +29,9 @@ import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executor;
|
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
|
// View
|
||||||
private final View rootView;
|
private final View rootView;
|
||||||
|
@ -40,23 +44,22 @@ public class CustomCamera extends FrameLayout {
|
||||||
private LifecycleOwner mLifecycleOwner;
|
private LifecycleOwner mLifecycleOwner;
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
private ProcessCameraProvider cameraProvider;
|
private ProcessCameraProvider mCameraProvider;
|
||||||
private ImageCapture imageCapture;
|
private ImageCapture mImageCapture;
|
||||||
private ImageCapture.OutputFileOptions outputFileOptions;
|
private ListenableFuture<ProcessCameraProvider> mCameraProviderFuture;
|
||||||
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
|
private Uri mTempFileUri;
|
||||||
private ContentValues contentValues;
|
|
||||||
private String fileName;
|
|
||||||
private Uri tempFileUri;
|
|
||||||
|
|
||||||
public CustomCamera(@NonNull Context context) {
|
public CustomCamera(@NonNull Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
rootView = inflate(context, R.layout.custom_camera_view, this);
|
rootView = inflate(context, R.layout.custom_camera_view, this);
|
||||||
|
findIds();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomCamera(@NonNull Context context, @Nullable AttributeSet attrs) {
|
public CustomCamera(@NonNull Context context, @Nullable AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
// Aqui van los estilos y todo eso si se requiere
|
// Aqui van los estilos y todo eso si se requiere
|
||||||
rootView = inflate(context, R.layout.custom_camera_view, this);
|
rootView = inflate(context, R.layout.custom_camera_view, this);
|
||||||
|
findIds();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initCustomCamera(@NonNull Context context, Executor executor, LifecycleOwner lifecycleOwner) {
|
public void initCustomCamera(@NonNull Context context, Executor executor, LifecycleOwner lifecycleOwner) {
|
||||||
|
@ -67,13 +70,12 @@ public class CustomCamera extends FrameLayout {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initComponents() {
|
private void initComponents() {
|
||||||
findIds();
|
|
||||||
initializeCamera();
|
initializeCamera();
|
||||||
initializeListeners();
|
initializeListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void findIds() {
|
private void findIds() {
|
||||||
cameraViewFinder = rootView.findViewById(R.id.pvViewfinder);
|
cameraViewFinder = rootView.findViewById(R.id.pvCameraViewFinder);
|
||||||
takePictureBtn = rootView.findViewById(R.id.btnTakePicture);
|
takePictureBtn = rootView.findViewById(R.id.btnTakePicture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,51 +85,55 @@ public class CustomCamera extends FrameLayout {
|
||||||
|
|
||||||
// El callback se tiene que implementar del lado de la aplicacion integradora
|
// El callback se tiene que implementar del lado de la aplicacion integradora
|
||||||
public void capturePhoto(CustomCameraCallback callback) {
|
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 {
|
||||||
@Override
|
mImageCapture.takePicture(mIutputFileOptions, mExecutor, new ImageCapture.OnImageSavedCallback() {
|
||||||
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
|
@Override
|
||||||
tempFileUri = outputFileResults.getSavedUri();
|
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
|
||||||
callback.onPictureTakenSuccess("Image saved! " + tempFileUri);
|
mTempFileUri = outputFileResults.getSavedUri();
|
||||||
//Toast.makeText(mContext, "Image saved! " + tempFileUri, Toast.LENGTH_LONG).show();
|
callback.onPictureTakenSuccess("Image saved! " + mTempFileUri);
|
||||||
}
|
//Toast.makeText(mContext, "Image saved! " + tempFileUri, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(@NonNull ImageCaptureException exception) {
|
||||||
|
callback.onPictureTakenFailError(exception.getMessage());
|
||||||
|
//Toast.makeText(mContext, "Image Not Saved " + exception.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
callback.onPictureTakenFailError(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(@NonNull ImageCaptureException exception) {
|
|
||||||
callback.onPictureTakenFailError(exception.getMessage());
|
|
||||||
//Toast.makeText(mContext, "Image Not Saved " + exception.getMessage(), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private ImageCapture.OutputFileOptions getOutputFileOptions(String name) {
|
private ImageCapture.OutputFileOptions getOutputFileOptions(String name) {
|
||||||
contentValues = new ContentValues();
|
ContentValues mContentValues = new ContentValues();
|
||||||
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
|
mContentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
|
||||||
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
|
mContentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
|
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(
|
return new ImageCapture.OutputFileOptions.Builder(
|
||||||
mContext.getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues
|
mContext.getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mContentValues
|
||||||
).build();
|
).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeCamera() {
|
private void initializeCamera() {
|
||||||
cameraProviderFuture = ProcessCameraProvider.getInstance(mContext);
|
mCameraProviderFuture = ProcessCameraProvider.getInstance(mContext);
|
||||||
cameraProviderFuture.addListener(() -> {
|
mCameraProviderFuture.addListener(() -> {
|
||||||
try {
|
try {
|
||||||
cameraProvider = cameraProviderFuture.get();
|
mCameraProvider = mCameraProviderFuture.get();
|
||||||
startCustomCamera(cameraProvider);
|
startCustomCamera(mCameraProvider);
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -142,15 +148,37 @@ public class CustomCamera extends FrameLayout {
|
||||||
Preview preview = new Preview.Builder().build();
|
Preview preview = new Preview.Builder().build();
|
||||||
preview.setSurfaceProvider(cameraViewFinder.getSurfaceProvider());
|
preview.setSurfaceProvider(cameraViewFinder.getSurfaceProvider());
|
||||||
|
|
||||||
imageCapture = new ImageCapture.Builder().build();
|
mImageCapture = new ImageCapture.Builder().build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
cameraProvider.unbindAll();
|
cameraProvider.unbindAll();
|
||||||
|
|
||||||
cameraProvider.bindToLifecycle(mLifecycleOwner, cameraSelector, preview, imageCapture);
|
cameraProvider.bindToLifecycle(mLifecycleOwner, cameraSelector, preview, mImageCapture);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,26 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?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_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
<androidx.camera.view.PreviewView
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
android:id="@+id/pvViewfinder"
|
app:layout_constraintTop_toTopOf="parent">
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="0dp"
|
|
||||||
app:layout_constraintBottom_toTopOf="@id/llCameraButtons"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/llCameraButtons"
|
android:id="@+id/llCameraButtons"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="bottom"
|
||||||
android:gravity="center"
|
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
|
<androidx.appcompat.widget.AppCompatButton
|
||||||
android:id="@+id/btnTakePicture"
|
android:id="@+id/btnTakePicture"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Take Picture" />
|
android:text="Take Picture" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
</androidx.camera.view.PreviewView>
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
||||||
|
|
Loading…
Reference in a new issue