implementation 'androidx.camera:camera-camera2:1.1.0-alpha02'
implementation 'androidx.camera:camera-lifecycle:1.1.0-alpha02'
implementation 'androidx.camera:camera-view:1.0.0-alpha20'
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
import androidx.camera.core.ImageProxy;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainActivity extends AppCompatActivity {
private ExecutorService cameraExecutor;
private ImageCapture imageCapture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraExecutor = Executors.newSingleThreadExecutor();
PreviewView previewView = findViewById(R.id.previewView);
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindPreview(cameraProvider, previewView);
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}, ContextCompat.getMainExecutor(this));
}
private void bindPreview(ProcessCameraProvider cameraProvider, PreviewView previewView) {
Preview preview = new Preview.Builder().build().also(previewUseCase -> {
previewUseCase.setSurfaceProvider(previewView.createSurfaceProvider());
});
imageCapture = new ImageCapture.Builder().build();
cameraProvider.unbindAll();
Camera camera = cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, preview, imageCapture);
findViewById(R.id.captureButton).setOnClickListener(view -> captureImage());
}
private void captureImage() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
File outputFile = new File(getOutputDirectory(), sdf.format(System.currentTimeMillis()) + ".jpg");
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(outputFile).build();
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {
Toast.makeText(MainActivity.this, "Image captured!", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(ImageCaptureException error) {
error.printStackTrace();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
cameraExecutor.shutdown();
}
private File getOutputDirectory() {
return getExternalMediaDirs()[0];
}
}