<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Apoorv Nath Mishra</title>
    <description>The latest articles on DEV Community by Apoorv Nath Mishra (@apoorvmishra21).</description>
    <link>https://dev.to/apoorvmishra21</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F560845%2Fb11dce09-9641-452f-85e3-8d01a7caa515.jpeg</url>
      <title>DEV Community: Apoorv Nath Mishra</title>
      <link>https://dev.to/apoorvmishra21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/apoorvmishra21"/>
    <language>en</language>
    <item>
      <title>Capture With cameraX</title>
      <dc:creator>Apoorv Nath Mishra</dc:creator>
      <pubDate>Thu, 03 Jun 2021 06:46:21 +0000</pubDate>
      <link>https://dev.to/apoorvmishra21/capture-with-camerax-57am</link>
      <guid>https://dev.to/apoorvmishra21/capture-with-camerax-57am</guid>
      <description>&lt;p&gt;Hello Readers! This was the first time I was gonna write something on Medium. After roaming around for a month and drowning in numerous topics, I thought why not explore our little (aah! not so little) android.&lt;/p&gt;

&lt;p&gt;Nowadays, the hot talk in android is about and around the Jetpack Support Library, and why not, it has given the developers a seamless workflow and extensive control over what they design, without being frustrated about the device-specific nuisance.&lt;br&gt;
Today, I shall get you a tour of the workflow of a very recent addition to the jetpack library family that's the cameraX (hurrey!!!!!).&lt;/p&gt;
&lt;h2&gt;
  
  
  Dependencies &amp;amp; Permissions
&lt;/h2&gt;

&lt;p&gt;In the dependency block of the build.gradle(Module:app) file in the Gradle Scripts, add the following snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def camerax_version = “1.0.0-beta07”
implementation “androidx.camera:camera-camera2:$camerax_version”
implementation “androidx.camera:camera-lifecycle:$camerax_version”
implementation “androidx.camera:camera-view:1.0.0-alpha14”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, make sure to check for the compile options to be,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, in the AndroidManifest.xml file, add the user-permissions as,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;uses-feature android:name="android.hardware.camera.any" /&amp;gt;
&amp;lt;uses-permission android:name="android.permission.CAMERA" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sync the Gradle, and then you are good to go with your work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prompt User-Permissions
&lt;/h2&gt;

&lt;p&gt;If the user doesn’t give above-mentioned permissions to the app, nothing will work, hence with the launch of the app first check for the permissions and then proceed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Check if permissions granted, if yes, start camera, else, request for permissions.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};
private int REQUEST_CODE_PERMISSIONS = 1001;
private boolean allPermissionsGranted(){

    for(String permission : REQUIRED_PERMISSIONS){
        if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
            return false;
        }
    }
    return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if(requestCode == REQUEST_CODE_PERMISSIONS){
        if(allPermissionsGranted()){
            startCamera();
        } else{
            Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, once we have all the required permissions, we shall start the camera, for that implement the startCamera() function.&lt;/p&gt;

&lt;h2&gt;
  
  
  startCamera()
&lt;/h2&gt;

&lt;p&gt;Now we shall work over the Activity class for camera.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PreviewView mPreviewView;
final ListenableFuture&amp;lt;ProcessCameraProvider&amp;gt; cameraProviderFuture;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, inside the &lt;strong&gt;onCreate()&lt;/strong&gt; method, initialize the instance variables and bind a camera provider so that we can bind the &lt;a href="https://developer.android.com/reference/androidx/camera/core/ImageAnalysis"&gt;image analysis&lt;/a&gt; case to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void startCamera() {

    final ListenableFuture&amp;lt;ProcessCameraProvider&amp;gt; cameraProviderFuture = ProcessCameraProvider.getInstance(this);

    cameraProviderFuture.addListener(() -&amp;gt; {
            try {
                ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                bindPreview(cameraProvider);
            } catch (ExecutionException | InterruptedException e) {
                // No errors need to be handled for this Future.
                // This should never be reached.
            }

    }, ContextCompat.getMainExecutor(this));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we shall bind the image analysis to the camera provider created in the &lt;strong&gt;onCreate&lt;/strong&gt; method and listens for changes in the camera’s rotation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bindPreview(ProcessCameraProvider)
void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
    Preview preview = new Preview.Builder()
            .build();

    CameraSelector cameraSelector = new CameraSelector.Builder()
            .requireLensFacing(camInstance)
            //.requireLensFacing(CameraSelector.LENS_FACING_BACK)
            .build();

    ImageAnalysis imageAnalysis = new ImageAnalysis.Builder().build();

    ImageCapture.Builder builder = new ImageCapture.Builder();

    ImageCapture imageCapture = builder.build();

    preview.setSurfaceProvider(mPreviewView.getSurfaceProvider());

    Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview, imageCapture);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now finally, if we look into the output results, in the Camera Activity, we can see the camera-enabled.&lt;/p&gt;

&lt;p&gt;Finally, we shall work on the capturing of the image.&lt;/p&gt;

&lt;h2&gt;
  
  
  Image Capture
&lt;/h2&gt;

&lt;p&gt;The image capture use case is designed for capturing high-resolution, high-quality photos and provides auto-white-balance, auto-exposure, and auto-focus (3A) functionality, in addition to simple manual camera controls. The caller is responsible for deciding how to use the captured picture, including the following options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;takePicture(Executor, OnImageCapturedCallback):&lt;/strong&gt; This method provides an in-memory buffer of the captured image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;takePicture(OutputFileOptions, Executor, OnImageSavedCallback):&lt;/strong&gt; This method saves the captured image to the provided file location.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This time, we shall work on the second caller constructor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;imageCapture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback () {})&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;File file = new File(mDateFormat.format(new Date())+ ".jpg");
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(file).build();
imageCapture.takePicture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback () {
   @Override
   public void onImageSaved(@NonNull ImageCapture.OutputFileResults      outputFileResults) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() { 
       //do whatever you want with the image saved in &amp;lt;file&amp;gt;.
 }
    });
}
@Override
public void onError(@NonNull ImageCaptureException error) {
    error.printStackTrace();
}
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;I have not mentioned anything about the activity layouts.&lt;br&gt;
Refer to my &lt;a href="https://github.com/APOORVMISHRA21/cameraApp"&gt;GitHub repo&lt;/a&gt; for the same, in case you need more insights.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;I hope you might find this article worthy assistant for beginning with CameraX.&lt;br&gt;
Drop your suggestions, for this, is my first article here :)&lt;/p&gt;

</description>
      <category>android</category>
      <category>jetpack</category>
      <category>java</category>
    </item>
  </channel>
</rss>
