0% found this document useful (0 votes)
14 views

Camera App Code

The code implements a basic camera app with an XML layout containing a SurfaceView and button to capture photos, a MainActivity class to initialize the camera, capture photos, and display results, and a manifest declaring camera permissions.

Uploaded by

ghanshampawar25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Camera App Code

The code implements a basic camera app with an XML layout containing a SurfaceView and button to capture photos, a MainActivity class to initialize the camera, capture photos, and display results, and a manifest declaring camera permissions.

Uploaded by

ghanshampawar25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Camera app code

Xml code -:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">

<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<Button
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:onClick="captureImage" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />

</RelativeLayout>

Mainactivity.java
package com.example.cameraapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


private static final int REQUEST_IMAGE_CAPTURE = 1;

private SurfaceView surfaceView;


private ImageView imageView;
private Button btnCapture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

// Initialize the SurfaceHolder and add a callback


SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// Initialize the camera here
initializeCamera();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// Handle surface changes, if needed
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Release the camera here
releaseCamera();
}
});
}

private void initializeCamera() {


// You should implement camera initialization code here
// This may involve using Camera API or CameraX for newer Android
versions
}

private void releaseCamera() {


// You should implement camera release code here
// This may involve releasing Camera API resources or CameraX
}

public void captureImage(View view) {


Intent takePictureIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) !=
null) {
startActivityForResult(takePictureIntent,
REQUEST_IMAGE_CAPTURE);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode ==
RESULT_OK && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap imageBitmap = (Bitmap) extras.get("data");
if (imageBitmap != null) {
displayCapturedImage(imageBitmap);
}
}
}
}

private void displayCapturedImage(Bitmap imageBitmap) {


surfaceView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(imageBitmap);
}
}

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

You might also like