0

I'm trying to add QR code scanning functionality to my application. I've used google vision to detect the QR codes. But, it fails to detect the codes. Even though the Detector.isOperational() returns true but nothing happening. I guess the preview quality is pretty dumb to scan the QR that's why it is failing. The codes as follow. THANK YOU.

package com.example.tngrocery;

import androidx.appcompat.app.AppCompatActivity;

   import android.graphics.SurfaceTexture;

   import android.hardware.Camera;

   import android.os.Bundle;

   import android.util.SparseArray;

   import android.view.Gravity;

   import android.view.TextureView;

   import android.widget.TextView;

   import android.widget.Toast;

   import com.google.android.gms.vision.Detector;

   import com.google.android.gms.vision.barcode.Barcode;

   import com.google.android.gms.vision.barcode.BarcodeDetector;

   public class ScanQR extends AppCompatActivity implements TextureView.SurfaceTextureListener{

TextureView textureView;

   TextureView textureView;

   TextView displayResult;

   BarcodeDetector detector;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_scan_q_r);

   displayResult = findViewById(R.id.alignQR);

   textureView = findViewById(R.id.texture_view);

   textureView.setSurfaceTextureListener(this);

   detector = new BarcodeDetector.Builder(this).

   setBarcodeFormats(Barcode.QR_CODE).

   build();

   try {

   if (detector.isOperational()) {

   displayResult.setText("it works");

   } else {

   displayResult.setText("Nope");

   }

   }

   catch (Exception e){

   Toast test = Toast.makeText(this,e.toString(),Toast.LENGTH_LONG);

   test.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);



test.show();
    }

   }

       @Override


       public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

       try{

       cam = Camera.open();

       test.show();


   }

   @Override

   public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

   try{

   cam = Camera.open();

   cam.setPreviewTexture(surface);

   cam.setDisplayOrientation(90);

   cam.startPreview();

   detector.setFocus(1);

   detector.setProcessor(new Detector.Processor<Barcode>(){

   @Override

   public void release() {

   }

   @Override

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

   final SparseArray<Barcode> barcodes = detections.getDetectedItems();

   if(barcodes.size() != 0){

   displayResult.post(new Runnable() {

   @Override

       public void run() {

   displayResult.setText(barcodes.valueAt(0).displayValue);

   }

   });

   }

   }

   });

   }

   catch(Exception camException){

   Toast.makeText(this,camException.toString(),Toast.LENGTH_LONG).show();

   }

   }

   @Override

   public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

   }

   @Override

   public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {

   cam.stopPreview();

   return false;

   }

   @Override

   public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

}
Trouble Maker
  • 99
  • 1
  • 5
  • Could you upload a properly formatted version of your code? In Android Studio on the left there's your file tree -> right click on your file -> Reformat code – m.reiter Apr 06 '21 at 14:55
  • But, there's no "reformat code" option in the file hierarchy – Trouble Maker Apr 06 '21 at 15:23
  • Uhm, does anything happen when you're in your ScanQr-Class and press a shortcut found here: https://stackoverflow.com/questions/16580171/code-formatting-shortcuts-in-android-studio-for-operation-systems – m.reiter Apr 06 '21 at 15:39
  • you can see my code now and i've formatted it properly just like you said.https://codeshare.io/5NwkpV – Trouble Maker Apr 06 '21 at 15:59

0 Answers0