Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.unej.sisterfortendik.activity.wajah;
- import static com.unej.sisterfortendik.config.util.FileUtils.getPath;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.ImageDecoder;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.util.Log;
- import android.util.Size;
- import android.view.OrientationEventListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- 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.ImageAnalysis;
- import androidx.camera.core.ImageCapture;
- import androidx.camera.core.ImageCaptureException;
- import androidx.camera.core.ImageProxy;
- import androidx.camera.core.Preview;
- import androidx.camera.lifecycle.ProcessCameraProvider;
- import androidx.camera.view.PreviewView;
- import androidx.core.content.ContextCompat;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
- import androidx.lifecycle.LifecycleOwner;
- import com.google.common.util.concurrent.ListenableFuture;
- import com.unej.sisterfortendik.R;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Executor;
- public class CameraActivity extends AppCompatActivity {
- private PreviewView previewView;
- private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
- private TextView textView;
- private ImageView im_capture_face;
- private ImageCapture imageCapture;
- private String TAG = "capturee";
- private Button btn_capture_face;
- @SuppressLint("RestrictedApi")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_camera2);
- previewView = findViewById(R.id.previewView);
- im_capture_face = findViewById(R.id.im_capture_face);
- cameraProviderFuture = ProcessCameraProvider.getInstance(this);
- textView = findViewById(R.id.orientation);
- btn_capture_face = findViewById(R.id.btn_capture_face);
- imageCapture = new ImageCapture.Builder()
- .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
- .setTargetResolution(new Size(512, 512))
- .setMaxResolution(new Size(1024,1024))
- .build();
- cameraProviderFuture = ProcessCameraProvider.getInstance(this);
- cameraProviderFuture.addListener(() -> {
- try {
- ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
- // startCameraX(cameraProvider);
- bindImageAnalysis(cameraProvider);
- } catch (ExecutionException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }, getExecutor());
- btn_capture_face.setOnClickListener(v -> {
- capturePhoto();
- });
- }
- private void bindImageAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
- ImageAnalysis imageAnalysis =
- new ImageAnalysis.Builder()
- .setTargetResolution(new Size(512, 512))
- .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
- .build();
- imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new ImageAnalysis.Analyzer() {
- @Override
- public void analyze(@NonNull ImageProxy image) {
- image.close();
- }
- });
- OrientationEventListener orientationEventListener = new OrientationEventListener(this) {
- @Override
- public void onOrientationChanged(int orientation) {
- textView.setText(Integer.toString(orientation));
- }
- };
- orientationEventListener.enable();
- Preview preview = new Preview.Builder().build();
- CameraSelector cameraSelector = new CameraSelector.Builder()
- .requireLensFacing(CameraSelector.LENS_FACING_FRONT).build();
- preview.setSurfaceProvider(previewView.createSurfaceProvider());
- cameraProvider.bindToLifecycle(
- this, cameraSelector, preview, imageCapture, imageAnalysis);
- }
- private Executor getExecutor() {
- return ContextCompat.getMainExecutor(this);
- }
- private void capturePhoto() {
- long timeStamp = System.currentTimeMillis();
- ContentValues contentValues = new ContentValues();
- contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, timeStamp);
- contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
- imageCapture.takePicture(
- new ImageCapture.OutputFileOptions.Builder(
- getContentResolver(),
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
- contentValues
- ).build(),
- getExecutor(),
- new ImageCapture.OnImageSavedCallback() {
- @Override
- public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
- Toast.makeText(CameraActivity.this,"Saving...",Toast.LENGTH_SHORT).show();
- // Log.e(TAG, "file > "+getContentResolver());
- // Log.e(TAG, "file > "+outputFileResults.getSavedUri());
- Uri uri = outputFileResults.getSavedUri();
- if (uri == null) {
- Toast.makeText(CameraActivity.this, "Saved URI should not be null.", Toast.LENGTH_SHORT).show();
- return;
- }
- File file = new File(getPath(uri));//uri to path
- // Log.e(TAG, "path file >: "+file );
- // Log.e(TAG, "path uri >: "+uri );
- Intent intent = new Intent();
- intent.putExtra("keyName", file.toString());
- intent.putExtra("uri", uri.toString());
- setResult(RESULT_OK, intent);
- finish();
- // Bitmap bitmap = null;
- // ContentResolver contentResolver = getContentResolver();
- // try {
- // bitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri);
- // Log.e(TAG, "path bitmap if : "+bitmap );
- //
- // ByteArrayOutputStream stream = new ByteArrayOutputStream();
- // bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
- // byte[] byteArray = stream.toByteArray();
- //
- // Intent intent = new Intent();
- // intent.putExtra("keyName", file.toString());
- // intent.putExtra("uri", uri.toString());\
- // setResult(RESULT_OK, intent);
- // finish();
- //
- // } catch (IOException e) {
- // throw new RuntimeException(e);
- // }
- }
- @Override
- public void onError(@NonNull ImageCaptureException exception) {
- Toast.makeText(CameraActivity.this,"Error: "+exception.getMessage(),Toast.LENGTH_SHORT).show();
- Log.e(TAG, exception.getMessage());
- }
- });
- }
- public String getPath(Uri uri)
- {
- String[] projection = { MediaStore.Images.Media.DATA };
- Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
- if (cursor == null) return null;
- int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- String s=cursor.getString(column_index);
- cursor.close();
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement