简体   繁体   English

使用 Android 的 Zxing 条码阅读器定义字符串大小

[英]Define String size using Android's Zxing barcode reader

I'm trying to set the string's length using a barcode reader.我正在尝试使用条形码阅读器设置字符串的长度。 My problem is: I'm trying to create a MAC address barcode reader, encoded as barcode from some computer devices.我的问题是:我正在尝试创建一个 MAC 地址条形码阅读器,从某些计算机设备编码为条形码。 So, every MAC Address has 12 digits and the barcode must read only 12 digits' strings in order to behave correctly.因此,每个 MAC 地址都有 12 位数字,并且条形码必须只能读取 12 位数字的字符串才能正确运行。 My code so far:到目前为止我的代码:

private void initiateScan(){
        IntentIntegrator integrator = IntentIntegrator.forSupportFragment(this);
        integrator.addExtra("PROMPT_MESSAGE","Put the Mac address at the center");
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        integrator.initiateScan();
}

onActivityResult:活动结果:

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            Context context = view.getContext();
            if(result.getContents() == null) {
                Log.d("MainActivity", "Cancelled scan");
                Toast.makeText(context, "Cancelled", Toast.LENGTH_SHORT).show();
            } else {
                Log.d("MainActivity", "Scanned");
                String content = result.getContents();
                Log.d(TAG,"scanned:: "+content);
                if(content.length() == UtilConstants.MAC_ADDRESS_SIZE){
                    if(field!=null)
                        field.setText(content);
                }else{
                    // invalid string size, reopen reader
                    Toast.makeText(getContext(),"Invalid code",Toast.LENGTH_SHORT).show();
                    initiateScan();
                }
            }
        } else {
            // This is important, otherwise the result will not be passed to the fragment
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

I don't think this is possible yet because the general aim for barcode is to encode and decode but another way you can achieve this is to use a particular barcode format to generate and the same time scan your barcode.我不认为这是可能的,因为条形码的一般目标是编码解码,但实现这一目标的另一种方法是使用特定的条形码格式生成并同时扫描您的条形码。 Barcodes come with their specific capacity ie how much content it can store or how much content can be encoded so consider using the EAN/UPC barcodes this type of barcodes stores maximum 13.条形码具有其特定容量,即它可以存储多少内容或可以编码多少内容,因此请考虑使用EAN/UPC 条形码,这种类型的条形码最多可存储 13 个。

Representing a MAC address with 12 digits means that the characters used in each digit will be hexadecimal numbers 0-9 and AF(af).用 12 位数字表示 MAC 地址意味着每个数字中使用的字符将是十六进制数字 0-9 和 AF(af)。

In that case, all of 1D product code, ITF, Codabar, RSS14 cannot be used.在这种情况下,无法使用所有 1D 产品代码、ITF、Codabar、RSS14。
Code128 and RSS14_Expanded are not good uses because the data contents to be used are mainly determined. Code128 和 RSS14_Expanded 不好用,因为主要是确定要使用的数据内容。

If you use it within the support range of Zxing, it will be 1D Industrial Code 39, Code 93, and 2D barcode QR Code, DataMatrix, MaxiCode, Aztec, PDF 417.如果在Zxing支持范围内使用,则为一维工业码39、93码、二维条码二维码、DataMatrix、MaxiCode、Aztec、PDF 417。
zxing/zxing zxing/zxing

There is ALLOWED_LENGTHS in DecodeHintType parameter of Zxing decodeRaw,decode API, so please give it a try. Zxing decodeRaw的DecodeHintType参数中有ALLOWED_LENGTHS,解码API,请试一试。
Class Code39Reader Class Code39Reader
Class Code93Reader Class Code93Reader
Class QRCodeReader Class QRCodeReader

Enum DecodeHintType 枚举解码提示类型
ALLOWED_LENGTHS ALLOWED_LENGTHS

public static final DecodeHintType ALLOWED_LENGTHS公共 static 最终 DecodeHintType ALLOWED_LENGTHS
Allowed lengths of encoded data -- reject anything else.允许的编码数据长度 - 拒绝其他任何内容。 Maps to an int[].映射到一个 int[]。

There are other options that affect the reading format and notification format, so please try it.还有其他选项会影响阅读格式和通知格式,请尝试一下。

However, there seems to be no function to limit the character code to hexadecimal characters, so even if the number of digits to be read can be specified, it is necessary to check the character code range after that.不过好像没有 function 将字符代码限制为十六进制字符,所以即使可以指定要读取的位数,也需要检查之后的字符代码范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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