How to Capture Image using Camera in Android Kotlin.

android {
buildFeatures {
viewBinding true
}
}
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Camera"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="30dp" />

<ImageView
android:id="@+id/img_viewer"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/btn_camera"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.camera

import android.content.ActivityNotFoundException
import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import com.example.camera.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
val REQUEST_IMAGE_CAPTURE = 1
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.btnCamera.setOnClickListener {
dispatchTakePictureIntent()
}

}
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
binding.imgViewer.setImageBitmap(imageBitmap)
}
}
}
Image capture using camera.

--

--

--

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter Bloc Architecture

Saving StateFlow State in ViewModel

League Bug Causing Champs To Peroidolicy Take Dmg From Xerith

My Boy Pro Mod Apk 2022

Chapter 5: Disposables and Specific Observables: Single/MayBe/Completable

Understanding Android touch flow control

Making Simple Android Chat App

RecyclerView with Compose View is not Performant

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Harshita Bambure

Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .

More from Medium

How to implement In-App-Review Structure on Android?

Android Tutorial: The Activity Lifecycle

Kotlin with MVVM application PART 2

Building a Live-tracking App using Google Maps in Android