QR扫描程序如何从远程数据库中检索数据?

时间:2015-11-22 11:28:43

标签: java android json database barcode-scanner

首先,我目前正在使用Android Studio和ZBAR lib来开发QR扫描仪。

1)QR码将包含唯一ID,例如车辆编号。

2)扫描二维码时,结果不仅会显示车辆编号,还会显示车辆名称,车辆所有者等。

总之,我正在尝试在激活qr代码扫描程序时通过唯一ID从远程数据库中检索数据。我知道我可以使用JSON解析但我不确定在代码中输入JSON解析的方式和位置。如果有人可以指导我或推荐一个好的教程,我将不胜感激。请注意我是编码的初学者。非常感谢您的帮助。

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);
?>

0 个答案:

没有答案