Search code examples
androidfirebaseandroid-studiokotlinfirebase-mlkit

How can I use a scanned variable (Barcode) in an SQL query


I am trying to use a scanned barcode as a variable in a SQL query. I am using the Google ML Kit Quick Start project. I have a connection and query function made using jtds1.3.1. I just cant get the raw value to be used in the query.

How could I call the query function with the raw value once the barcode has been scanned? How could I store the raw value in a variable each time a barcode is scanned?

override fun onSuccess(
        originalCameraImage: Bitmap?,
        barcodes: List<FirebaseVisionBarcode>,
        frameMetadata: FrameMetadata,
        graphicOverlay: GraphicOverlay
    ) {
        graphicOverlay.clear()

        originalCameraImage?.let {
            val imageGraphic = CameraImageGraphic(graphicOverlay, it)
            graphicOverlay.add(imageGraphic)
        }

        barcodes.forEach {
            val barcodeGraphic = BarcodeGraphic(graphicOverlay, it)
            graphicOverlay.add(barcodeGraphic)
        }
        graphicOverlay.postInvalidate()
    }

Solution

  • You probably want something along these lines:

    override fun onSuccess(
            originalCameraImage: Bitmap?,
            barcodes: List<FirebaseVisionBarcode>,
            frameMetadata: FrameMetadata,
            graphicOverlay: GraphicOverlay) {
    
        String barcodeValue = barcodes.get(0).getRawValue();
    
        PreparedStatement stmt = dbConnection.createStatement("insert into scanned_barcodes(barcode) values (?)");
        stmt.setString(1, barcodeValue);
        stmt.execute();
    }
    

    The scanned barcodes are in the barcodes list. The value can be accessed with getRawValue().

    The rest of the code is regular JDBC code: create a statement, bind the current value and execute it.