通過掃碼器掃描條形碼,並解析條形碼(python版本/C++版本)

         掃碼器從本質上可以被視爲鍵盤設備,利用程序實時讀入掃取的序列號,然後,將號碼發送到代請求網站上,返回Json數據中包含商品信息。

python版本

# -*- coding: utf-8 -*-
"""
Created on Fri Aug 30 15:05:58 2019

@author: xiaoxiaoke
"""

import sys, os
from pynput.keyboard import Controller,Key,Listener
import requests
import json
import numpy

# 監聽按壓
def on_press(key):    
    try:
         global str1
         str1 =str1+key.char       
         if len(str1)==13:
              url = 'https://www.mxnzp.com/api/barcode/goods/details?barcode='     
              url1=url+str1
              require1 = requests.get(url1)
              result1 = json.loads(require1.text)
              print(result1) #['data']['goodsName']
              print(result1['data']['goodsName'])
              str1=""    
    except AttributeError:
        return False

# 監聽釋放
def on_release(key):
    if key==Key.esc:
        # 停止監聽
        return False
 
# 開始監聽
def start_listen():
    with Listener(on_press=on_press,on_release=on_release) as listener:
        listener.join()

if __name__ == '__main__':
    str1=''
    start_listen()

C++版本

1、首先,查看設備號:

cat /proc/bus/input/devices

2、替換/dev/input/event10中的設備號。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <cstring>

struct input_event buff;
int fd;
int read_nu; 
char chr_table[12] = {
[0] = '\0', [1] = '*', [2] = '1', [3] = '2', [4] = '3', 
[5] = '4', [6] = '5', [7] = '6', [8] = '7', [9] = '8',
[10] = '9',[11] = '0'};
char barcode[13];
int main(int argc, char *argv[])
{
    
    fd = open("/dev/input/event10", O_RDONLY);
    if (fd < 0)
    {
        printf("can not open device usbkeyboard!");
    }
    int i = 2;
    while (1)
    {
        while (read(fd, &buff, sizeof(struct input_event)) == 0)
        {
            printf("barcode is NULL");
        }

        if (buff.type == EV_KEY)
	{
            if (buff.value == 0)
		{
                //printf("type:%d code:%c \n", buff.type, chr_table[buff.code]);
                barcode[i]=chr_table[buff.code];
		printf("num=%d barcode:%c \n", i,barcode[i]);

		if(i==12)
		{
		  i=2;
		 // printf("barcode:%s\n", barcode);
		}
		i++;
		}
	}     
    }
 
    close(fd);
    return 1;
}

解析條形碼的內容:

#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/curlbuild.h>
#include <sstream>
#include <iostream>
#include <boost/foreach.hpp>
#include <string>
#include <ctime>
#include <unistd.h>
//#include "json\json.h"  

using namespace std;

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
    string data((const char*) ptr, (size_t) size * nmemb);
    *((stringstream*) stream) << data << endl;
    return size * nmemb;
}

int main(int argc, char *argv[])
{
 
        /*HTTP GET json data*/
        std::stringstream out;
        void* curl = curl_easy_init();
        // 設置URL
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.mxnzp.com/api/barcode/goods/details?barcode=6925843404256");
        // 設置接收數據的處理函數和存放變量
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);

        // 執行HTTP GET操作
        CURLcode res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }

        // 接受數據存放在out中,輸出之
        string str_json = out.str();
        printf("json數組:%s\n",str_json.c_str());
        JSONObject json = JSONObject.fromObject(str_json);
        curl_easy_cleanup(curl);
   
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章