Write a Program to capture an image and display it using Image View.

 Java:

package com.example.myexperiments;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
int CameraPhotoRequestCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
Intent i1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);              startActivityForResult(i1,CameraPhotoRequestCode);

}
});

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

ImageView im1 = (ImageView) findViewById(R.id.im1);
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CameraPhotoRequestCode){
Bitmap photo = (Bitmap) data.getExtras().get("data");
im1.setImageBitmap(photo);
        }
    }
}




XML:

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

    <ImageView
        android:id="@+id/im1"
        android:layout_width="410dp"
        android:layout_height="410dp"
        />

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Capture and Set Image"
        android:textSize="25dp"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        />
</LinearLayout>

Output:



Comments

Popular posts from this blog

Write a Program to accept and display the student information

Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.

Write a program to create a first display screen on any search engine using auto complete text View