How to capture image from Camera in Android Studio with Example
MainActivity.java
package com.learnoset.testproject;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements ActivityResultCallback<ActivityResult> {
private ActivityResultLauncher<Intent> activityResultLauncher;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
// Register for Activity Result Launcher
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this);
// Take Photo
dispatchTakePictureIntent();
}
private void dispatchTakePictureIntent() {
// Creating Take Photo Intent to launch Camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// launch camera to take photo
activityResultLauncher.launch(takePictureIntent);
}
@Override
public void onActivityResult(ActivityResult result) {
// Getting photo data
Bundle extras = result.getData().getExtras();
// getting bitmap from photo data
Bitmap imageBitmap = (Bitmap) extras.get("data");
// setting bitmap to the ImageView
imageView.setImageBitmap(imageBitmap);
}
}AndroidManifest.xml
<!-- Adding camera permissions in the AndroidManifest.xml file-->
<uses-feature android:name="android.hardware.camera"
android:required="true" />
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
Gamer Bazi - Tournament Application with Admob Ads & Web Admin Panel
Login & Register Screen UI Design - 01
Settings and Profile page UI design in Android Studio with Example and Source Code
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.
