0% found this document useful (0 votes)
4 views2 pages

Camera code using existing camera and display image in app

The document provides code for an Android application that captures images using the device's camera and displays them in an ImageView. It includes XML layout code for the user interface, which consists of an ImageView and a button to capture images, as well as Java code in MainActivity to handle camera intents and display the captured image. Additionally, it specifies the necessary permissions in the manifest file to access the camera hardware.

Uploaded by

pareshloq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Camera code using existing camera and display image in app

The document provides code for an Android application that captures images using the device's camera and displays them in an ImageView. It includes XML layout code for the user interface, which consists of an ImageView and a button to capture images, as well as Java code in MainActivity to handle camera intents and display the captured image. Additionally, it specifies the necessary permissions in the manifest file to access the camera hardware.

Uploaded by

pareshloq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Camera code using existing camera and display image in app

Xml code -:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:background="@android:color/darker_gray"/>

<Button
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image"
android:layout_marginTop="16dp"/>

</LinearLayout>

MainActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ImageView imageView;


private ActivityResultLauncher<Intent> cameraLauncher;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);
Button btnCapture = findViewById(R.id.btnCapture);

cameraLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
Bundle extras = result.getData().getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
});

btnCapture.setOnClickListener(v -> {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraLauncher.launch(intent);
});
}
}

Manifest File

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>

You might also like