Introduction :
In the photography world, the maximum people use camera of smartphone to capture life’s moments, express their creativity and communicate visually with others. There are default camera app is inbuilt in the smartphone by which you can view and capturing rectangular photo and video. Now a days, people want customize camera app for various purposes such as sharing photos to social media platforms.
One of them is circular or rounded camera app that gives you a fun and creative way to capture and share your moments in social media. Creating a custom rounded camera application using Android Studio could be an exciting project for new developers. In this article, I shall show you how create a circular or rounded camera application using Android Studio IDE with source code. To create such app you have to add rounded corners to your camera preview.
What is rounded camera app :
Generally, mobile camera app is designed to capture rectangular images or videos. The camera app which displays circular frame on the preview screen and capture circular images or videos is called circular or rounded camera application. The primary feature of a circular or rounded camera app is capturing rounded photos or videos within a circular frame for more visually appealing look.
There are other features you can find in circular or rounded camera app such as flash control, exposure adjustment, filters, editing tools (effects, stickers) and more. You can also share your rounded photos or videos directly to social media platforms (Facebook, Instagram etc.) or other messaging app.
About the application :
This is a customize camera app where you can see circular or rounded camera preview will be displayed on the screen. When you take a photo using the button, the rounded photo will be saved on your phone. To view the rounded photo, you have to open “My Files” or “File Manager” of your phone.
Step for create the application :
If you follow the below step, you can successfully build a rounded or circular camera on your phone.
Step 1 :
At first, open the Android Studio on your pc. Now, create a new project with empty activity and name the project such as “Camera_Round_Application” You have to check on legacy android.support libraries. If you do not know how to create Android app, you can follow my link.
Step 2 :
After some times, you can see a default project is opened on your Android Studio IDE. First, open the MainActivity.java file and replace the default code with the following code.
package com.example.camera_round_application;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
finish();
}
}, 5000);
}
else {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
finish();
}
}
}
Step 3 :
After that, open the activity_main.xml file and replace the default code with the following code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="#14B3F8"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="313dp"
android:layout_marginEnd="16dp"
android:textColor="#FA1111"
android:layout_marginBottom="399dp"
android:padding="10dp"
android:text="Wait for some times"
android:textSize="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Step 4 :
Now, create a new empty activity keep the default name (MainActivity2). Open MainActivity2.java and paste the below code in it.
package com.example.camera_round_application;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Environment;
import android.support.v7.widget.CardView;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity2 extends Activity {
private Camera camera;
private Round_camera_preview round_preview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
camera = getCameraInstance();
round_preview = new Round_camera_preview(this, camera);
CardView round_view = (CardView) findViewById(R.id.round_cardview);
round_view.addView(round_preview);
Button take_photo = (Button) findViewById(R.id.button_take);
take_photo.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
camera.takePicture(null, null, picture);
}catch (Exception E){
Toast.makeText(MainActivity2.this, "pppppppp", Toast.LENGTH_SHORT).show();
}
}
}
);
}
public static Camera getCameraInstance() {
Camera round_camera = null;
try {
round_camera = Camera.open();
round_camera.startPreview();
} catch (Exception e) {
}
return round_camera;
}
@Override
protected void onPause() {
super.onPause();
camera.release();
}
Camera.PictureCallback picture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
String round_file = "rounded_photo" + System.currentTimeMillis() + ".jpg";
File picture_file = Environment.getExternalStoragePublicDirectory(round_file);
if (picture_file == null) {
return;
}
FileOutputStream fos = new FileOutputStream(picture_file);
Bitmap bitmap=BitmapFactory.decodeByteArray(data,0,data.length);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap output_bitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output_bitmap);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, round_preview.getCameraDistance()/2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
Bitmap rotatedBitmap = Bitmap.createBitmap(output_bitmap, 0, 0, output_bitmap.getWidth(), output_bitmap.getHeight(), matrix, true);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
camera.startPreview();
} catch (Exception e) {
}
}
} ;
public class Round_camera_preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder round_holder;
private Camera Round_surface_camera;
public Round_camera_preview(Context context, Camera camera) {
super(context);
Round_surface_camera = camera;
try {
Round_surface_camera.setDisplayOrientation(90);
}catch (Exception e){
}
round_holder = getHolder();
round_holder.addCallback(this);
round_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
Round_surface_camera.setPreviewDisplay(holder);
Round_surface_camera.startPreview();
} catch (Exception e) {
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (round_holder.getSurface() == null) {
return;
}
try {
Round_surface_camera.stopPreview();
} catch (Exception e) {
}
try {
Round_surface_camera.setPreviewDisplay(round_holder);
Round_surface_camera.startPreview();
} catch (Exception e) {
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Round_surface_camera.release();
Round_surface_camera = null;
}
}
}
Step 5 :
Then, open the activity_main2.xml file and replace the default code with the following code.
<?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:background="#14B3F8"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<LinearLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_height="0dp">
<android.support.v7.widget.CardView
android:id="@+id/round_cardview"
android:layout_width="200dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_height="200dp"
app:cardCornerRadius="100dp"
android:elevation="50dp">
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="0dp">
<Button
android:id="@+id/button_take"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="25dp"
android:textColor="#FA0505"
android:background="#FF9800"
android:text="TAKE PHOTO" />
</LinearLayout>
</LinearLayout>
Step 6 :
Then, open build.gradle(Module) file and add the following code in dependencies. After adding the code click on “Sync Now”.
implementation 'com.android.support:cardview-v7:28.0.0'
Step 7 :
Lastly, open the AndroidManifest.xml file. Here, you have to add the camera and storage permission. So you can add the following code inside the manifest tag. It should be before the application tag.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After that, you have to also add the below code between the tag.
android:largeHeap="true"
After adding the all above code, the AndroidManifest.xml file looks like below code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camera_round_application">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/Theme.Camera_Round_Application">
<activity android:name=".MainActivity2"></activity>
<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>
Run the application :
Now, click the green “Run” button in the Android Studio IDE after connect your phone to computer via USB cable (data cable). Then, the app will be installed and launch on your device. You can see your rounded camera app on your phone like this image.

Conclusion :
At last, you have learned how to build customize rounded camera app using Android Studio IDE. Now, you can add more feature in the app to create more realistic rounded camera app. Thank you for visiting my site.