简体   繁体   中英

How can a QR Scanner retrieve data from remote database?

Firstly, I am currently using Android Studio and ZBAR lib to develop a QR Scanner.

1) QR Code will contain the unique id for example the vehicle number.

2) When the QR code are scanned, the result will not only show the vehicle number but also for example name of vehicle, owner of vehicle etc

In conclusion, i am trying to retrieve data from the remote database through unique ID when qr code scanner are activated. I know i can use JSON parse but i am not sure of how and where to input JSON parse in the codes. I would appreciate if someone can guide me or recommend a good tutorial. Please do take note i am a beginner in coding. Thank you very much for your help.

QRScanner.java package com.example.namirah.eparking.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.namirah.eparking.R;

import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;


public class QRScanner extends Activity {
public static final String TAG = QRScanner.class.getSimpleName();


private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;

private Button scanButton;
private ImageScanner scanner;

private boolean barcodeScanned = false;
private boolean previewing = true;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.qr_scanner);


    initControls();

}

private void initControls() {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    autoFocusHandler = new Handler();
    mCamera = getCameraInstance();

    // Instance barcode scanner
    scanner = new ImageScanner();
    scanner.setConfig(0, Config.X_DENSITY, 3);
    scanner.setConfig(0, Config.Y_DENSITY, 3);

    mPreview = new CameraPreview(QRScanner.this, mCamera, previewCb,
            autoFocusCB);
    FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
    preview.addView(mPreview);

    scanButton = (Button) findViewById(R.id.ScanButton);

    scanButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (barcodeScanned) {
                barcodeScanned = false;
                mCamera.setPreviewCallback(previewCb);
                mCamera.startPreview();
                previewing = true;
                mCamera.autoFocus(autoFocusCB);

            }
        }
    });
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        releaseCamera();
    }
    return super.onKeyDown(keyCode, event);
}


/**
 * A safe way to get an instance of the Camera object.
 */
public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e) {
    }
    return c;
}

private void releaseCamera() {
    if (mCamera != null) {
        previewing = false;
        mCamera.setPreviewCallback(null);
        mCamera.release();
        mCamera = null;
    }
}

private Runnable doAutoFocus = new Runnable() {
    public void run() {
        if (previewing)
            mCamera.autoFocus(autoFocusCB);
    }
};

public Camera.PreviewCallback previewCb = new Camera.PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = parameters.getPreviewSize();

        Image barcode = new Image(size.width, size.height, "Y800");
        barcode.setData(data);

        final int result = scanner.scanImage(barcode);

        if (result != 0) {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();

            SymbolSet syms = scanner.getResults();
            for (Symbol sym : syms) {

                Log.i("<<<<<<Asset Code>>>>> ",
                        "<<<<Bar Code>>> " + sym.getData());
                final String scanResult = sym.getData().trim();


                Intent intent = new Intent(QRScanner.this, QRCodeDetails.class);
                intent.putExtra("result", scanResult);
                startActivity(intent);

              /*  Toast.makeText(BarcodeScanner.this, scanResult, Toast.LENGTH_SHORT).show();*/

                barcodeScanned = true;

                break;
            }


        }

    }
};


    // Mimic continuous auto-focusing
    Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
            autoFocusHandler.postDelayed(doAutoFocus, 1000);
        }
    };

};

qrcodedetails.php

<?php
 //open connection to mysql db
$connection =mysqli_connect("localhost","dbname","passsword") or die("Error " . mysqli_error($connection));

//fetch table rows from mysql db
$vehicleno = $_GET['vehicle_no'];
$sql = "SELECT * FROM parkingsession where vehicle_no = '$vehicleno' ";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result)) //row contain result
{
    $emparray[] = $row;
}
echo json_encode($emparray);

//close the db connection
mysqli_close($connection);
?>

You need to make a http request from the android application to your php server with the scanned ID in the request string (just as if you had typed the request string into the browser's url bar).

Given that you understand that this is what you want to do, take a look at Make an HTTP request with android which outlines how you can do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM