Implement Custom Camera View like PhonePe in your Application

Nowadays, Everyone is using Applications like Phonepe, GooglePay, Paytm for payment as they are providing a convenient and faster way of payment just by scanning the QR Code.
One of my clients has also asked me to implement QR code scanning for the payment in his application. According to me Look and feel of the application is also important. So let’s customize the camera view for QR code scanning. And implement Camera API to scan the QR code.
Before we move to coding I am sharing one preview image.
Firstly, Let’s start with Custom Camera View Like PhonePe. And then we will implement the Camera API.
Add dependency of Google vision in build.gradle(:app)
implementation ' is used to draw a rectangle on canvas.
public class RectangleOverLayView extends LinearLayout {

   private Bitmap bitmap;

   public RectangleOverLayView(Context context) {



   public RectangleOverLayView(Context context, AttributeSet attrs) {

       super(context, attrs);


   public RectangleOverLayView(Context context, AttributeSet attrs, int defStyleAttr) {

       super(context, attrs, defStyleAttr);


   public RectangleOverLayView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

       super(context, attrs, defStyleAttr, defStyleRes);



   protected void dispatchDraw(Canvas canvas) {


       if (bitmap == null) {



       canvas.drawBitmap(bitmap, 0, 0, null);


   protected void createWindowFrame() {


       setLayerType(LAYER_TYPE_HARDWARE, null);

       bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);

       Canvas osCanvas = new Canvas(bitmap);

       RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());

       Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);


       osCanvas.drawRect(outerRectangle, paint);


       paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

       float x1 = getWidth() / 5;

       float x2 = getWidth() - x1;

       float heightDiff = x2 - x1;

       float y1 = (getHeight() / 2) - (heightDiff / 2);

       float y2 = (getHeight() / 2) + (heightDiff / 2);

       RectF rect = new RectF(x1, y1, x2, y2);

       osCanvas.drawRoundRect(rect, 100, 100, paint);

       RectF rectBorder = new RectF(x1 + 10, y1 + 10, x2 - 10, y2 - 10);




       osCanvas.drawRoundRect(rectBorder, 90, 90, paint);



   public boolean isInEditMode() {

       return true;



   protected void onLayout(boolean changed, int l, int t, int r, int b) {

       super.onLayout(changed, l, t, r, b);

       bitmap = null;




<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""








       android:layout_height="match_parent" />








       app:layout_constraintTop_toTopOf="@+id/surfaceCamera" />





       app:layout_constraintLeft_toLeftOf="@+id/txtScanCode" />






       app:layout_constraintTop_toTopOf="parent" />

Camera View with the Rectangle in the center is done. So let’s start with the Camera source to scan the QR code. This camera source is a part of the android-vision( This one is the easiest and best way of scanning without any other third party.
First add camera permission in AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />

public class CameraPreview extends Fragment implements SurfaceHolder.Callback, Detector.Processor<Barcode> {

   private CameraSource cameraSource;

   private BarcodeDetector barcodeDetector;

   private SurfaceView surfaceCamera;



   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

       super.onCreateView(inflater, container, savedInstanceState);

       return inflater.inflate(R.layout.camera_preview, container, false);



   public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

       super.onViewCreated(view, savedInstanceState);

       surfaceCamera = view.findViewById(;



   public void surfaceCreated(SurfaceHolder holder) {

       //Ask for runtime camera permission before call startCameraSurface




   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {



   public void surfaceDestroyed(SurfaceHolder holder) {



   public void release() {



   public void receiveDetections(Detector.Detections<Barcode> detections) {

       SparseArray<Barcode> barcode = detections.getDetectedItems();

       if ( barcode.size()>0){

           Log.e("CameraPreview"," QR Code : "+barcode.valueAt(0).displayValue);



   private void setBarCodeListeners() {

       barcodeDetector = new BarcodeDetector.Builder(getActivity())



       DisplayMetrics displayMetrics = new DisplayMetrics();


       cameraSource = new CameraSource.Builder(getActivity(), barcodeDetector)

               .setRequestedPreviewSize(displayMetrics.heightPixels, displayMetrics.widthPixels)






   private void startCameraSurface() {


       if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

           // TODO: Consider calling

           //    ActivityCompat#requestPermissions

           // here to request the missing permissions, and then overriding

           //   public void onRequestPermissionsResult(int requestCode, String[] permissions,

           //                                          int[] grantResults)

           // to handle the case where the user grants the permission. See the documentation

           // for ActivityCompat#requestPermissions for more details.



       try {


       } catch (IOException e) {




   private void stopSurface(){




   public void onPause() {






   public void onStart() {




There are many third party libraries available on Git for Qr code Scanning. But the best practice to scan the QR code is google vision apis. Using these APIs you can achieve many features like face tracker, eye tracker, etc.
Hopefully this helps you to develop the scanning QR code functionality in an easy way.

Subscribe to our newsletter

Related Posts

Leave a Comment