CameraX का इस्तेमाल शुरू करना

इस कोडलैब में हम जानेंगे कि कैसे कैमरा ऐप्लिकेशन बनाया जाता है, जिससे व्यूफ़ाइंडर को दिखाने, फ़ोटो लेने, और कैमरे से इमेज स्ट्रीम का विश्लेषण करने में CameraX का इस्तेमाल होता है.

ऐसा करने के लिए, हम CameraX में इस्तेमाल के उदाहरणों से जुड़ी जानकारी देंगे. इसका इस्तेमाल कई तरह के कैमरे की कार्रवाइयों के लिए किया जा सकता है. इसमें रीयल टाइम में व्यूफ़ाइंडर दिखाने से लेकर फ़्रेम का विश्लेषण करने तक शामिल है.

हम क्या सीखेंगे

  • CameraX डिपेंडेंसी जोड़ने का तरीका.
  • किसी गतिविधि में कैमरा की झलक दिखाने का तरीका. (इस्तेमाल के उदाहरण की झलक)
  • फ़ोटो लेने का तरीका, इसे स्टोरेज में सेव करना. (ImageCapture इस्तेमाल का उदाहरण)
  • रीयल टाइम में कैमरे से फ़्रेम का विश्लेषण करने का तरीका. (इमेज विश्लेषण का इस्तेमाल का उदाहरण)

हमें हार्डवेयर की ज़रूरत होगी

  • एक Android डिवाइस, हालांकि Android Studio' का एम्युलेटर ठीक से काम करेगा. कम से कम 21 एपीआई लेवल काम करता है.

हमें सॉफ़्टवेयर की ज़रूरत होगी

  • Android Studio 3.3 या इसके बाद वाले वर्शन.

Android Studio के मेन्यू का इस्तेमाल करके, एक नया प्रोजेक्ट शुरू करें. पूछे जाने पर, खाली गतिविधि चुनें.

इसके बाद, हम अपनी पसंद का कोई भी नाम चुन सकते हैं. हमें यह पक्का करना चाहिए कि भाषा Kotlin पर सेट हो, कम से कम एपीआई लेवल 21 हो (CameraX के लिए कम से कम ज़रूरी लेवल है) और हम AndroidX आर्टफ़ैक्ट का इस्तेमाल करते हैं.

शुरू करने के लिए, हमें डिपेंडेंसी सेक्शन में जाकर, Camera App की डिपेंडेंसी को अपने ऐप्लिकेशन की Gradle फ़ाइल में जोड़ने दें:

// Use the most recent version of CameraX, currently that is alpha04
def camerax_version = "1.0.0-alpha04"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"

जब कहा जाए, तब अभी सिंक करें पर क्लिक करें और हम अपने ऐप्लिकेशन में CameraX का इस्तेमाल करने के लिए तैयार हो जाएंगे.

कैमरा व्यूफ़ाइंडर दिखाने के लिए, हम SurfaceTexture का इस्तेमाल करेंगे. इस कोडलैब में हम निश्चित साइज़ के स्क्वेयर फ़ॉर्मैट में व्यूफ़ाइंडर दिखाएंगे. रिस्पॉन्सिव व्यूफ़ाइंडर के ज़्यादा बेहतर उदाहरण के लिए आधिकारिक सैंपल की चेकआउट करें.

Res > layout_gt; activity_main.xml: के तहत, Activity_main लेआउट फ़ाइल में बदलाव करने दें:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextureView
            android:id="@+id/view_finder"
            android:layout_width="640px"
            android:layout_height="640px"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

कैमरे का इस्तेमाल करने वाले हमारे प्रोजेक्ट में किसी भी फ़ंक्शन को जोड़ने के लिए, कैमरा अनुमतियों का सही अनुरोध करना. सबसे पहले, हमें ऐप्लिकेशन टैग से पहले, मेनिफ़ेस्ट में इनका एलान करना होगा:

<uses-permission android:name="android.permission.CAMERA" />

इसके बाद, MainActivity के अंदर, हमें रनटाइम के दौरान अनुमतियों का अनुरोध करना होगा. हम java > com.example.cameraxapp > MainActivity.kt: के तहत, MainActivity फ़ाइल में बदलाव करेंगे:

फ़ाइल में सबसे ऊपर, MainActivity क्लास की परिभाषा के बाहर, ये कॉन्सटेंट और इंपोर्ट जोड़ें:

// Your IDE likely can auto-import these classes, but there are several
// different implementations so we list them here to disambiguate
import android.Manifest
import android.util.Size
import android.graphics.Matrix
import java.util.concurrent.TimeUnit

// This is an arbitrary number we are using to keep tab of the permission
// request. Where an app has multiple context for requesting permission,
// this can help differentiate the different contexts
private const val REQUEST_CODE_PERMISSIONS = 10

// This is an array of all the permission specified in the manifest
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)

MainActivity क्लास में, ये फ़ील्ड और हेल्पर मैथड जोड़ें. इनका इस्तेमाल अनुमतियों का अनुरोध करने के लिए होता है. साथ ही, जब हम यह जान जाते हैं कि सभी अनुमतियां मिल गई हैं, तो हमारे कोड को ट्रिगर कर दिया जाता है:

class MainActivity : AppCompatActivity(), LifecycleOwner {

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
    }

    // Add this after onCreate

    private lateinit var viewFinder: TextureView

    private fun startCamera() {
        // TODO: Implement CameraX operations
    }

    private fun updateTransform() {
        // TODO: Implement camera viewfinder transformations
    }

    /**
     * Process result from permission request dialog box, has the request
     * been granted? If yes, start Camera. Otherwise display a toast
     */
    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        if (requestCode == REQUEST_CODE_PERMISSIONS) {
            if (allPermissionsGranted()) {
                viewFinder.post { startCamera() }
            } else {
                Toast.makeText(this,
                    "Permissions not granted by the user.", 
                    Toast.LENGTH_SHORT).show()
                finish()
            }
        }
    }

    /**
     * Check if all permission specified in the manifest have been granted
     */
    private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
        ContextCompat.checkSelfPermission(
               baseContext, it) == PackageManager.PERMISSION_GRANTED
    }
}

आखिर में, हम सही होने पर अनुमति अनुरोध को ट्रिगर करने के लिए onCreate में सब कुछ एक साथ डाल देते हैं:

override fun onCreate(savedInstanceState: Bundle?) {
    ...

    // Add this at the end of onCreate function

    viewFinder = findViewById(R.id.view_finder)

    // Request camera permissions
    if (allPermissionsGranted()) {
        viewFinder.post { startCamera() }
    } else {
        ActivityCompat.requestPermissions(
            this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
    }

    // Every time the provided texture view changes, recompute layout
    viewFinder.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
        updateTransform()
    }
}

अब, ऐप्लिकेशन शुरू होने पर, यह देखा जाएगा कि इसके पास कैमरे की ज़रूरी अनुमतियां हैं या नहीं. अगर ऐसा होता है, तो यह `startCamera()` सीधे कॉल करेगा. अगर ऐसा नहीं है, तो इससे अनुमतियों का अनुरोध किया जाएगा. अनुमति मिलने के बाद, `startCamera()` कॉल करें.

ज़्यादातर कैमरा ऐप्लिकेशन के लिए, उपयोगकर्ताओं को एक व्यूफ़ाइंडर दिखाना बहुत ज़रूरी है -- नहीं तो उपयोगकर्ताओं के लिए कैमरे को सही जगह पर सेट करना बहुत मुश्किल होता है. CameraX `झलक` क्लास का इस्तेमाल करके व्यूफ़ाइंडर लागू किया जा सकता है.

झलक देखने के लिए, हमें सबसे पहले एक कॉन्फ़िगरेशन तय करना होगा. इसका इस्तेमाल, उपयोग के उदाहरण को बनाने के लिए किया जाएगा. इससे मिलने वाला इंस्टेंस वह है जिसे हमें CameraX लाइफ़साइकल से जोड़ना होगा. हम इसे `startCamera()` तरीके से करेंगे; लागू करने के लिए इस कोड का इस्तेमाल करें:

private fun startCamera() {

    // Create configuration object for the viewfinder use case
    val previewConfig = PreviewConfig.Builder().apply {
        setTargetAspectRatio(Rational(1, 1))
        setTargetResolution(Size(640, 640))
    }.build()

    // Build the viewfinder use case
    val preview = Preview(previewConfig)

    // Every time the viewfinder is updated, recompute layout
    preview.setOnPreviewOutputUpdateListener {

        // To update the SurfaceTexture, we have to remove it and re-add it
        val parent = viewFinder.parent as ViewGroup
        parent.removeView(viewFinder)
        parent.addView(viewFinder, 0)

        viewFinder.surfaceTexture = it.surfaceTexture
        updateTransform()
    }

    // Bind use cases to lifecycle
    // If Android Studio complains about "this" being not a LifecycleOwner
    // try rebuilding the project or updating the appcompat dependency to
    // version 1.1.0 or higher.
    CameraX.bindToLifecycle(this, preview)
}

इस समय, हमें रहस्यमयी `updateTransform()` तरीका लागू करना होगा. `updateTransform()` के अंदर, हमारा मकसद डिवाइस के ओरिएंटेशन में बदलाव करना है. इससे, हमारे व्यूफ़ाइंडर को सीधी रोटेशन में दिखाया जा सकता है:

private fun updateTransform() {
    val matrix = Matrix()

    // Compute the center of the view finder
    val centerX = viewFinder.width / 2f
    val centerY = viewFinder.height / 2f

    // Correct preview output to account for display rotation
    val rotationDegrees = when(viewFinder.display.rotation) {
        Surface.ROTATION_0 -> 0
        Surface.ROTATION_90 -> 90
        Surface.ROTATION_180 -> 180
        Surface.ROTATION_270 -> 270
        else -> return
    }
    matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)

    // Finally, apply transformations to our TextureView
    viewFinder.setTransform(matrix)
}

प्रोडक्शन के लिए तैयार ऐप्लिकेशन लागू करने के लिए, आधिकारिक नमूने पर एक नज़र डालें और देखें कि और क्या चीज़ें संभाली जानी चाहिए. इस कोडलैब को छोटा रखने के लिए, हम कुछ शॉर्टकट ले रहे हैं. उदाहरण के लिए, हम 180-डिग्री वाले डिवाइस को घुमाने जैसे कुछ कॉन्फ़िगरेशन में हुए बदलावों पर नज़र नहीं रख रहे हैं. इन बदलावों से, हमें लेआउट बदलने वाले लिसनर को ट्रिगर नहीं करना पड़ता. गैर-स्क्वेयर व्यूफ़ाइंडर को डिवाइस के घुमाने पर, आसपेक्ट रेशियो या चौड़ाई-ऊंचाई के अनुपात में होने वाले बदलाव की भरपाई भी करनी होगी.

अगर हम ऐप्लिकेशन बनाते और चलाते हैं, तो हमें ज़िंदगी की झलक देखनी चाहिए. बढ़िया!

उपयोगकर्ताओं को इमेज कैप्चर करने की अनुमति देने के लिए, हम re&> लेआउट > activity_main.xml: में स्ट्रक्चर्ड व्यू के बाद लेआउट के हिस्से के रूप में एक बटन देंगे:

<ImageButton
        android:id="@+id/capture_button"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_margin="24dp"
        app:srcCompat="@android:drawable/ic_menu_camera"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

झलक दिखाने की तुलना में, इस्तेमाल के दूसरे उदाहरण काफ़ी हद तक काम करते हैं. सबसे पहले, हमें एक कॉन्फ़िगरेशन ऑब्जेक्ट तय करना होगा, जिसका इस्तेमाल रीयल-टाइम के इस्तेमाल के ऑब्जेक्ट को इंस्टैंशिएट करने के लिए किया जाता है. फ़ोटो कैप्चर करने के लिए, जब 'कैप्चर करें' बटन दबाया जाता है, तब हमें CameraX.bindToLifecycle पर कॉल करने से पहले, `startCamera()` तरीके को अपडेट करना होगा और आखिर में कोड की कुछ और लाइनें जोड़नी होंगी:

private fun startCamera() {

    ...

    // Add this before CameraX.bindToLifecycle

    // Create configuration object for the image capture use case
    val imageCaptureConfig = ImageCaptureConfig.Builder()
        .apply {
            setTargetAspectRatio(Rational(1, 1))
            // We don't set a resolution for image capture; instead, we
            // select a capture mode which will infer the appropriate
            // resolution based on aspect ration and requested mode
            setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
    }.build()

    // Build the image capture use case and attach button click listener
    val imageCapture = ImageCapture(imageCaptureConfig)
    findViewById<ImageButton>(R.id.capture_button).setOnClickListener {
        val file = File(externalMediaDirs.first(),
            "${System.currentTimeMillis()}.jpg")
        imageCapture.takePicture(file,
            object : ImageCapture.OnImageSavedListener {
            override fun onError(error: ImageCapture.UseCaseError,
                                 message: String, exc: Throwable?) {
                val msg = "Photo capture failed: $message"
                Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                Log.e("CameraXApp", msg)
                exc?.printStackTrace()
            }

            override fun onImageSaved(file: File) {
                val msg = "Photo capture succeeded: ${file.absolutePath}"
                Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                Log.d("CameraXApp", msg)
            }
        })
    }

    // Bind use cases to lifecycle
    // If Android Studio complains about "this" being not a LifecycleOwner
    // try rebuilding the project or updating the appcompat dependency to
    // version 1.1.0 or higher.
    CameraX.bindToLifecycle(this, preview)
}

इसके बाद, इस्तेमाल के नए उदाहरण को शामिल करने के लिए, CameraX.bindToLifecycle पर कॉल अपडेट करें:

CameraX.bindToLifecycle(this, preview, imageCapture)

इसी तरह, हमने एक सामान्य फ़ोटो लेने वाला बटन लागू किया था.

CameraX की एक सबसे दिलचस्प सुविधा, ImageAnalysis क्लास है. इसकी मदद से हम ImageAalysis.Analytics इंटरफ़ेस को लागू करने वाली कस्टम क्लास के बारे में बता सकते हैं. इसे आने वाले 'कैमरा फ़्रेम' के साथ कहा जाएगा. CameraX के मुख्य विज़न के मुताबिक, हमें कैमरे के सेशन की स्थिति को मैनेज करने या इमेज को नष्ट करने की चिंता नहीं करनी होगी. हमारे ऐप्लिकेशन के मनचाहे लाइफ़साइकल की लाइफ़ दूसरीलाइफ़साइकल के बारे में बताने वाले कॉम्पोनेंट की तरह ही काफ़ी है.

सबसे पहले, हम एक कस्टम इमेज एनालाइज़र लागू करेंगे. हमारा विश्लेषण करने वाला बहुत आसान है. यह सिर्फ़ इमेज की औसत रोशनी (चमक) को लॉग करता है. साथ ही, यह बताता है कि आर्बिट्ररी कॉम्प्लेक्स में इस्तेमाल के लिए क्या करना ज़रूरी है. हमें बस एक क्लास में `analyze` फ़ंक्शन को ओवरराइड करना है, जो ImageAnalysis.Analyticsr इंटरफ़ेस को लागू करता है. हम अपनी गतिविधि को MainActivity में एक अंदरूनी क्लास के तौर पर परिभाषित कर सकते हैं:

private class LuminosityAnalyzer : ImageAnalysis.Analyzer {
    private var lastAnalyzedTimestamp = 0L

    /**
     * Helper extension function used to extract a byte array from an
     * image plane buffer
     */
    private fun ByteBuffer.toByteArray(): ByteArray {
        rewind()    // Rewind the buffer to zero
        val data = ByteArray(remaining())
        get(data)   // Copy the buffer into a byte array
        return data // Return the byte array
    }

    override fun analyze(image: ImageProxy, rotationDegrees: Int) {
        val currentTimestamp = System.currentTimeMillis()
        // Calculate the average luma no more often than every second
        if (currentTimestamp - lastAnalyzedTimestamp >=
            TimeUnit.SECONDS.toMillis(1)) {
            // Since format in ImageAnalysis is YUV, image.planes[0]
            // contains the Y (luminance) plane
            val buffer = image.planes[0].buffer
            // Extract image data from callback object
            val data = buffer.toByteArray()
            // Convert the data into an array of pixel values
            val pixels = data.map { it.toInt() and 0xFF }
            // Compute average luminance for the image
            val luma = pixels.average()
            // Log the new luma value
            Log.d("CameraXApp", "Average luminosity: $luma")
            // Update timestamp of last analyzed frame
            lastAnalyzedTimestamp = currentTimestamp
        }
    }
}

इमेज क्लास का विश्लेषण करने के लिए ImageAalysis.Analytics इंटरफ़ेस लागू करना, अन्य सभी उपयोग मामलों की तरह हमें बस इमेज विश्लेषण को इंस्टैंशिएट करना है और CameraX.bindToLifecycle पर कॉल करने से पहले `startCamera()` फ़ंक्शन को एक बार फिर से अपडेट करें:

private fun startCamera() {

    ...

    // Add this before CameraX.bindToLifecycle

    // Setup image analysis pipeline that computes average pixel luminance
    val analyzerConfig = ImageAnalysisConfig.Builder().apply {
        // Use a worker thread for image analysis to prevent glitches
        val analyzerThread = HandlerThread(
            "LuminosityAnalysis").apply { start() }
        setCallbackHandler(Handler(analyzerThread.looper))
        // In our analysis, we care more about the latest image than
        // analyzing *every* image
        setImageReaderMode(
            ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
    }.build()

    // Build the image analysis use case and instantiate our analyzer
    val analyzerUseCase = ImageAnalysis(analyzerConfig).apply {
        analyzer = LuminosityAnalyzer()
    }

    // Bind use cases to lifecycle
    // If Android Studio complains about "this" being not a LifecycleOwner
    // try rebuilding the project or updating the appcompat dependency to
    // version 1.1.0 or higher.
    CameraX.bindToLifecycle(this, preview, imageCapture)
}

साथ ही, हम इस्तेमाल के नए उदाहरण के लिए, CameraX.bindtoLifecycle कॉल को अपडेट करते हैं:

CameraX.bindToLifecycle(
    this, preview, imageCapture, analyzerUseCase)

ऐप्लिकेशन चलाने पर, हर सेकंड के लॉगकैट में इससे मिलता-जुलता मैसेज दिखेगा:

D/CameraXApp: Average luminosity: ...

ऐप्लिकेशन की जांच करने के लिए, हमें Android Studio में, चलाएं बटन पर क्लिक करना होगा. हमारा प्रोजेक्ट चुने गए डिवाइस या एम्युलेटर में बनाया जाएगा, डिप्लॉय किया जाएगा, और लॉन्च किया जाएगा. ऐप्लिकेशन के लोड होने के बाद, हमें व्यूफ़ाइंडर दिखेगा, जो पहले से जोड़े गए ओरिएंटेशन-हैंडलिंग कोड की वजह से डिवाइस को घुमाने के बाद भी सीधा बना रहेगा. साथ ही, बटन का इस्तेमाल करके फ़ोटो ले सकेगा:

आपने कोड लैब को पूरा कर लिया है! पीछे देखते हुए, आपने शुरुआत से नए Android ऐप्लिकेशन में इन्हें लागू किया है:

  • आपके प्रोजेक्ट में CameraX डिपेंडेंसी शामिल की गई.
  • कैमरा व्यूफ़ाइंडर दिखाया गया (झलक इस्तेमाल करने के उदाहरण का इस्तेमाल करके)
  • इमेज कैप्चर किया गया, इमेज को स्टोरेज में सेव किया जा रहा है (ImageCapture के इस्तेमाल के उदाहरण का इस्तेमाल करके)
  • रीयल टाइम में कैमरे से फ़्रेम का विश्लेषण किया गया है (इमेज विश्लेषण के इस्तेमाल के उदाहरण का इस्तेमाल करके)

अगर आप CameraX और इसके साथ की जा सकने वाली चीज़ों के बारे में ज़्यादा जानना चाहते हैं, तो दस्तावेज़ देखें या आधिकारिक सैंपल का क्लोन बनाएं.