These are the following two ways, in which you can use camera in your application
Using existing android camera application in our application
Directly using Camera API provided by android in our application
Using existing android camera application in our application
You will use MediaStore.ACTION_IMAGE_CAPTURE to launch an existing camera application installed on your phone. Its syntax is given below
step 1:-
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:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
app:cardCornerRadius="50dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imgCamera"
android:src="@drawable/edtbg"
android:scaleType="fitXY"/>
<ImageView
android:layout_width="20dp"
android:src="@drawable/ic_baseline_add_a_photo_24"
android:layout_height="20dp"
android:layout_margin="15dp"
android:id="@+id/imgOpenCamera"
android:layout_gravity="end|bottom"/>
</androidx.cardview.widget.CardView>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="@drawable/edtbg"
android:id="@+id/edtName"
android:paddingStart="10dp"
android:hint="Enter Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edtbg"
android:layout_marginTop="5dp"
android:id="@+id/edtEmail"
android:paddingStart="10dp"
android:hint="Enter Email"/>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Save"
android:textColor="@color/white"
android:backgroundTint="#FF5722"
android:id="@+id/btnSave"/>
</LinearLayout>
step 2:-
MainActivity.java
package com.sandhya.cameraopen;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imgOpenCamera, imgCamera;
private static final int CAMERA_REQ_CODE = 100;
private static final int GALLERY_REQ_CODE = 100;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgCamera = findViewById(R.id.imgCamera);
imgOpenCamera = findViewById(R.id.imgOpenCamera);
imgOpenCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(iCamera, CAMERA_REQ_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQ_CODE) {
//for camera
Bitmap img = (Bitmap) (data.getExtras().get("data"));
imgCamera.setImageBitmap(img);
}
}
}
}
output:-

إرسال تعليق