Home > Back-end >  String to QR Code and vice versa in Android Studio
String to QR Code and vice versa in Android Studio

Time:04-23

I want to create an app that allows you to enter a text and create the qr code of that text or read a qr code via the phone's camera and get the text contained in it. is there any function that allows me to do this?

CodePudding user response:

  1. In your gradle, add - implementation 'androidmads.library.qrgenearator:QRGenearator:1.0.4'

  2. String to QR method-- In your activity:

import androidmads.library.qrgenearator.QRGContents;

import androidmads.library.qrgenearator.QRGEncoder;

public class classname extends activity {

private ImageView qrImage;
private Bitmap bitmap;
private QRGEncoder qrgEncoder;

String value = "Some string value";
onCreate(){

 qrImage = findViewById(R.id.qr_image);
 createqr(value);

}

 public void createqr(String QRvalue) {

    try {
            WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);
            int width = point.x;
            int height = point.y;
            int smallerDimension = width < height ? width : height;
            smallerDimension = smallerDimension * 3 / 4;

            qrgEncoder = new QRGEncoder(QRvalue, null, QRGContents.Type.TEXT, smallerDimension);
            qrgEncoder.setColorBlack(Color.BLACK);
            qrgEncoder.setColorWhite(Color.WHITE);
            try {
                bitmap = qrgEncoder.getBitmap();
                qrImage.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
  1. QR to String method---

implementation 'me.dm7.barcodescanner:zxing:1.9.13'

implementation 'me.dm7.barcodescanner:zbar:1.9.13'

public classname extends AppCompatActivity{

Button scanqr;
EditText qrvalue;
String QRContent ;

    oncreate(){

scanqr = findVIewById(R.id.scanqr);
qrvalue = findViewById(R.id.qrvals);

sscanqr.setOnclickListener(new View.OnClickListenrer(){

                Intent ie = new Intent(getContext(),zBarcodeScan.class);
                startActivityForResult(ie,BARCODE_REQUEST1);

})

}

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

 if(requestCode == BARCODE_REQUEST1 && resultCode == RESULT_OK) {
 QRContent = data.getStringExtra("BarcodeValue");
qrvalue.setText(QRContent);
}
}
  1. add zbarcodescan.java class in your project

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import androidx.annotation.Nullable;

import android.view.ViewGroup;

import android.view.WindowManager;

import java.util.ArrayList;

import java.util.List;

import me.dm7.barcodescanner.zbar.Result;

import me.dm7.barcodescanner.zbar.ZBarScannerView;

import me.dm7.barcodescanner.zbar.BarcodeFormat;

public class zBarcodeScan extends Activity implements ZBarScannerView.ResultHandler  {
//    public class zBarcodeScan extends Activity implements ZBarScannerView.ResultHandler {

    private ZBarScannerView mScannerView;
    private boolean barcodeScanned = false;
    private ArrayList<Integer> mSelectedIndices;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        setContentView(R.layout.activity_simple_scanner);
        ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
        mScannerView = new ZBarScannerView(this);
        mScannerView.setAutoFocus(true);
        setupFormats();
        contentFrame.addView(mScannerView);
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();
    }

    @Override
    public void onBackPressed() {
// TODO Auto-generated method stub
        if (barcodeScanned) {
            barcodeScanned = false;
            mScannerView.stopCamera();
        }
        Intent intent = new Intent();
        setResult(RESULT_CANCELED,intent);
        zBarcodeScan.this.finish();
    }


    public void setupFormats() {

        List<BarcodeFormat> formats = new ArrayList<BarcodeFormat>();
        if(mSelectedIndices == null || mSelectedIndices.isEmpty()) {
            mSelectedIndices = new ArrayList<Integer>();
            for(int i = 0; i < BarcodeFormat.ALL_FORMATS.size(); i  ) {
                mSelectedIndices.add(i);
            }
        }

        for(int index : mSelectedIndices) {
            formats.add(BarcodeFormat.ALL_FORMATS.get(index));
//            formats.add(BarcodeFormat.CODE128);
//            formats.add(BarcodeFormat.CODE39);
//            formats.add(BarcodeFormat.CODE93);
////            formats.remove(BarcodeFormat.DATABAR);
////            formats.remove(BarcodeFormat.I25);
//            formats.add(BarcodeFormat.QRCODE);
        }
        if(mScannerView != null) {
            mScannerView.setFormats(formats);
        }
    }
    @Override
    public void handleResult(Result rawResult) {
//        Toast.makeText(this, "Contents = "   rawResult.getContents()  
//                ", Format = "   rawResult.getBarcodeFormat().getName(), Toast.LENGTH_SHORT).show();
        // Note:
        // * Wait 2 seconds to resume the preview.
        // * On older devices continuously stopping and resuming camera preview can result in freezing the app.
        // * I don't know why this is the case but I don't have the time to figure out.
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScannerView.resumeCameraPreview(zBarcodeScan.this);
            }
        }, 2000);

        barcodeScanned = true;
        Intent intent = new Intent();
        intent.putExtra("BarcodeValue", rawResult.getContents());
        setResult(RESULT_OK,intent);
        zBarcodeScan.this.finish();
    }
}
  • Related