How to create a simple Android Camera App or Mirror App to preview the images using Android Studio?

This video shows the steps to create a simple camera application for android smartphone using android studio.

In this video, a very simple approach has been shown to do the application development. It just shows how we can use our phone camera to get the preview image.

Since we will be getting the preview, we can also use this App like a Mirror App which shows the image seen by front camera. And the smartphone screen can be used like mirror screen to show or project the mirror image.

The video take you slowly through the development showing how to code each line of the program. Towards the end it also shows how to do the setting of the emulator to use your Android virtual device (AVD), that is emulator, to test this App. So, it is suggested to watch this tutorial till the end.

For any questions please comment in this video and it will be answered as soon as possible.

If you like this video please subscribe to the channel for further update on development of similar Apps.

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source Code:

package com.example.mycamera;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

private CameraCaptureSession myCameraCaptureSession;
private String myCameraID;
private CameraManager myCameraManager;
private CameraDevice myCameraDevice;
private TextureView myTextrureView;
private CaptureRequest.Builder myCaptureRequestBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myTextrureView = findViewById(R.id.textureView);
myCameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
openCamera();
}

private CameraDevice.StateCallback myStateCallBack = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
myCameraDevice = camera;
}

@Override
public void onDisconnected(@NonNull CameraDevice camera) {
myCameraDevice.close();
}

@Override
public void onError(@NonNull CameraDevice camera, int error) {
myCameraDevice.close();
myCameraDevice = null;

}
};

private void openCamera() {
try {
myCameraID = myCameraManager.getCameraIdList()[0];

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
myCameraManager.openCamera(myCameraID, myStateCallBack, null);

}
catch (Exception e){
e.printStackTrace();
}
}

public void cameraPreview(View view){

SurfaceTexture mySurfaceTexture = myTextrureView.getSurfaceTexture();
Surface mySurface = new Surface(mySurfaceTexture);

try {
myCaptureRequestBuilder = myCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
myCaptureRequestBuilder.addTarget(mySurface);

myCameraDevice.createCaptureSession(Arrays.asList(mySurface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
myCameraCaptureSession = session;
myCaptureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
try {
myCameraCaptureSession.setRepeatingRequest(myCaptureRequestBuilder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {

}
}, null
);
} catch (CameraAccessException e) {
e.printStackTrace();
}

}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mycamera”>

<uses-permission android:name=”android.permission.CAMERA”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
<uses-permission android:name=”android.permission.RECORD_AUDIO”/>
<uses-feature android:name=”android.hardware.camera2″/>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<TextureView
android:id=”@+id/textureView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”150dp”
android:layout_marginTop=”250dp”
android:onClick=”cameraPreview”
android:text=”Capture” />
</RelativeLayout>

1 comment

Leave a Reply

%d bloggers like this: