android 二维码 Zxing记录

来源:互联网 发布:网络空间安全考研排名 编辑:程序博客网 时间:2024/05/18 16:16
参考资料:https://github.com/journeyapps/zxing-android-embedded 这个礼拜公司要一个生产二维码和扫描二维码的demo。因此花了2天时间去找二维码方面的资料,找了很多资料,大部分都推荐用google开源的Zxing的二维码库,因此也遇到很多坑,下载下来的代码不能扫描,或者是不能生产二维码,最后搞定特写一遍记录二维码的demo方便以后用,同时也希望可以帮助大家。关于google Zxing的源码在github上面 地址:https://github.com/zxing/zxing 。二维码其实就是用一张图片来记录一些信息,如uri等信息,利用图片的算法来把信息用图片记录。当扫描的时候就或者图片记录的信息,做进一步的处理。一。生成二维码1.首先需要添加Zxing的core_3.2.0.jar包 在添加到libs下面。 public void generateQR(){    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();    BitMatrix matrix = null;    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    String text = et_targetText.getText().toString();    if (text.length() == 0){        Log.e(TAG,"text cannot be empty");        return;    }    try {        matrix = new MultiFormatWriter().encode(text,                BarcodeFormat.QR_CODE, 120, 120);    } catch (WriterException e) {        e.printStackTrace();    }    int width = matrix.getWidth();    int height = matrix.getHeight();    int[] pixels = new int[width * height];    for (int y = 0; y < height; y++) {        for (int x = 0; x < width; x++) {            if (matrix.get(x, y)) {                pixels[y * width + x] = BLACK;            }        }    }    bitmap = Bitmap.createBitmap(width, height,            Bitmap.Config.ARGB_8888);    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);    iv_displayQR.setImageBitmap(bitmap);}从代码中可以看到最后生成的是一个Bitmap。二。扫描二维码。 public void scanQR(){    new IntentIntegrator(this).initiateScan();}在这里已经封装好了,直接调用。因为android 6.0开始的runtime权限的问题需要申请camera的权限。接下来会调用public Intent createScanIntent() {    Intent intentScan = new Intent(activity, getCaptureActivity());    intentScan.setAction(Intents.Scan.ACTION);    // check which types of codes to scan for    if (desiredBarcodeFormats != null) {        // set the desired barcode types        StringBuilder joinedByComma = new StringBuilder();        for (String format : desiredBarcodeFormats) {            if (joinedByComma.length() > 0) {                joinedByComma.append(',');            }            joinedByComma.append(format);        }        intentScan.putExtra(Intents.Scan.FORMATS, joinedByComma.toString());    }    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);    attachMoreExtras(intentScan);    return intentScan;}可以看到进入了CaptureActivity进行扫描。扫描后的结果用一个回调到了IntentResult。获取回调结果: protected void onActivityResult(int requestCode, int resultCode, Intent data) {    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);    if(result != null) {        if(result.getContents() == null) {            Log.d("MainActivity", "Cancelled scan");            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();        } else {            Log.d("MainActivity", "Scanned");            et_qrContent.setText(result.getContents());        }    } else {        // This is important, otherwise the result will not be passed to the fragment        super.onActivityResult(requestCode, resultCode, data);    }}记录完毕,扫描阶段就是一个图形识别,想对图像识别研究可以下载Zxing的源码去研究下此demo的源码:https://github.com/GithubRyze/ZxingDemo
1 0
原创粉丝点击