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

Practical 23

The document provides two exercises for mobile application development: one for capturing and displaying an image using an ImageView, and another for recording video using various camera methods. It includes XML layout files and Java code for both functionalities, detailing the implementation of camera access, image capture, and video recording. The code demonstrates the use of Android's camera API and UI components to facilitate these tasks.

Uploaded by

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

Practical 23

The document provides two exercises for mobile application development: one for capturing and displaying an image using an ImageView, and another for recording video using various camera methods. It includes XML layout files and Java code for both functionalities, detailing the implementation of camera access, image capture, and video recording. The code demonstrates the use of Android's camera API and UI components to facilitate these tasks.

Uploaded by

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

Subject :- Mobile Application Development Subject Code :- 22617

Name :- Shivam Sainath Korpakwad Batch :- CO 6 IA Roll No. 24

Exercise :-

1). Write a program to capture an image and display it using image view.

Code :-

Activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Take a Photo" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/button1"
android:layout_alignParentTop="true" />
</RelativeLayout>

MainActivity.java
package com.example.oo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle; i
mport android.view.Menu; import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener()
{
@Override public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Output :-
2). Write a program to record a video using various camera methods.

Code :-

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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/recordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Start Recording" />
</RelativeLayout>
MainActivity.java
package com.example.oo;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.TotalCaptureResult;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log; import android.util.Size;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
private static final String TAG = "CameraRecording";
private static final int REQUEST_CAMERA_PERMISSION = 200; private static final int MEDIA_TYPE_VIDEO = 1;
private SurfaceView surfaceView;
private Button recordButton;
private CameraManager cameraManager;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSession;
private Size videoSize;
private String cameraId;
private HandlerThread backgroundThread;
private Handler backgroundHandler;
private boolean isRecording = false;
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
recordButton = findViewById(R.id.recordButton); surfaceView.getHolder().addCallback(surfaceCallback);
recordButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (isRecording)
{
stopRecording();
}
else
{
startRecording();
}
}
});
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try
{
cameraId = cameraManager.getCameraIdList()[0];
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED)
{
openCamera();
}
else
{
requestCameraPermission();
}
}
private void requestCameraPermission()
{
ActivityCompat.requestPermissions( this, new String[]
{
Manifest.permission.CAMERA
},
REQUEST_CAMERA_PERMISSION );
}
private void openCamera()
{
try
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED)
{
cameraManager.openCamera(cameraId, stateCallback, backgroundHandler);
}
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
}
private void startRecording()
{
// Implement video recording logic here // For demonstration purposes, let's just log a message Log.d(TAG,
"Recording started");
isRecording = true; recordButton.setText("Stop Recording");
}
private void stopRecording()
{
// Implement video recording stop logic here // For demonstration purposes, let's just log a message
Log.d(TAG, "Recording stopped");
isRecording = false;
recordButton.setText("Start Recording");
}
private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback()
{
@Override
public void surfaceCreated(SurfaceHolder holder)
{
//
The Surface has been created, acquire the camera and start the preview. openCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
// Do nothing
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// Do nothing
}
};
private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback()
{
@Override
public void onOpened(@NonNull CameraDevice camera)
{
// This method is called when the camera is opened. Start camera preview here.
cameraDevice = camera; createCameraPreviewSession();
}
@Override public void onDisconnected(@NonNull CameraDevice camera)
{
camera.close();
cameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice camera, int error)
{
camera.close();
cameraDevice = null;
}
};
private void createCameraPreviewSession()
{
try
{
Surface surface = surfaceView.getHolder().getSurface();
cameraDevice.createCaptureSession( Collections.singletonList(surface), new
CameraCaptureSession.StateCallback()
{
@Override
public void onConfigured(@NonNull CameraCaptureSession session)
{ cameraCaptureSession = session; updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session)
{
// Handle configuration failure
}
},
backgroundHandler
);
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
} private void updatePreview()
{
try
{
CaptureRequest.Builder captureRequestBuilder =
cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surfaceView.getHolder().getSurface());
cameraCaptureSession.setRepeatingRequest( captureRequestBuilder.build(), null, backgroundHandler );
}
catch (CameraAccessException e) { e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
grantResults)
{
if (requestCode == REQUEST_CAMERA_PERMISSION)
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
openCamera();
}
else
{
// Handle permission denial
}
}
}
@Override
protected void onResume()
{ super.onResume();
startBackgroundThread();
if (surfaceView.getHolder().getSurface() != null)
{
openCamera();
}
else
{
surfaceView.getHolder().addCallback(surfaceCallback);
}
}
@Override protected void onPause()
{
closeCamera();
stopBackgroundThread();
super.onPause();
}
private void startBackgroundThread()
{
backgroundThread = new HandlerThread("CameraBackground"); backgroundThread.start();
backgroundHandler = new Handler(backgroundThread.getLooper());
}
private void stopBackgroundThread()
{
if (backgroundThread != null)
{
backgroundThread.quitSafely();
try { backgroundThread.join();
backgroundThread = null;
backgroundHandler = null;
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private void closeCamera()
{
if (cameraCaptureSession != null)
{
cameraCaptureSession.close();
cameraCaptureSession = null;
}
if (cameraDevice != null)
{
cameraDevice.close();
cameraDevice = null;
}
}
}

Output :-

You might also like