0% found this document useful (0 votes)
18 views4 pages

Image Capture and Display

Uploaded by

HARSH MAGHNANI
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)
18 views4 pages

Image Capture and Display

Uploaded by

HARSH MAGHNANI
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/ 4

MainActivity.

java

package com.example.cameraapp;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;

import android.provider.MediaStore;

import android.widget.ImageView;

import android.widget.Button;

import android.view.View;

import android.content.Intent;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

ImageView img;

Button button ;

private static final int REQUEST_IMAGE_CAPTURE = 101;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

img = findViewById(R.id.img);

button = findViewById(R.id.button);

button.setOnClickListener(v -> { dispatchTakePictureIntent();});

private void dispatchTakePictureIntent(){

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


if(intent.resolveActivity(getPackageManager())!=null) {

startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

protected void onActivityResult(int requestCode, int resultCode, Intent data){

super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){

Bundle extras = data.getExtras();

assert extras != null;

Bitmap bitmap =(Bitmap) extras.get("data");

img.setImageBitmap(bitmap);

activity_main.xml:

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

<LinearLayout 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"

android:orientation="vertical"

android:gravity="center"

tools:context=".MainActivity">

<ImageView

android:id="@+id/img"
android:layout_width="match_parent"

android:layout_height="400dp"

android:layout_marginTop="80dp"

android:contentDescription="@string/captured_image"

android:foregroundGravity="center"

android:padding="0dp"

tools:srcCompat="@tools:sample/avatars" />

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/open_camera"

android:layout_marginTop="20dp"

/>

</LinearLayout>

Manifest file:

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools">

<uses-feature

android:name="android.hardware.camera"

android:required="false" />

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

android:allowBackup="true"

android:dataExtractionRules="@xml/data_extraction_rules"

android:fullBackupContent="@xml/backup_rules"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/Theme.CameraApp"

tools:targetApi="31">

<activity

android:name=".MainActivity"

android:exported="true">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

Note: can also write only uses feature part instead of whole manifest file.

You might also like