简体   繁体   English

使用Zxing的Android条码扫描器

[英]Android Barcode scanner using Zxing

I want to integrate Zxing in a android project (I'm a beginner). 我想将Zxing集成到android项目中(我是初学者)。

I have seen that the way described here http://bit.ly/nBszrL is discouraged and that the best way to do it is through Intents as said in this post http://bit.ly/o29Uma 我已经看到不鼓励在此描述http://bit.ly/nBszrL的方式,并且最好的方法是通过Intents来实现,如本文http://bit.ly/o29Uma中所述

ps : I don't want the barcode scanner installed on my device ps:我不想在设备上安装条形码扫描仪

I have Included on my project the required class : http://bit.ly/16pKMKx 我已经在我的项目中包含了所需的类: http : //bit.ly/16pKMKx

my test code : 我的测试代码:

package com.example.barcodescanner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final int REQUEST_BARCODE = 0;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // when my button is clicked
    public void scanBarCode(View view) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "PRODUCT_MODE");  

        startActivityForResult(intent, REQUEST_BARCODE);

        Toast toast = Toast.makeText(this, "Start scanning Barcode", Toast.LENGTH_SHORT);
        toast.show();
    }

}

and I really don't understand how to do it by this way : http://bit.ly/18v7K2O (I really don't get it and that's what I want to use) 而且我真的不明白该怎么做: http : //bit.ly/18v7K2O (我真的不明白,这就是我要使用的东西)

Do you have any idea about how to do it ? 你有关于如何做的任何想法吗?

Thank you. 谢谢。

The Android system was built to so that people could write apps that do one particular thing well and other developers could use them when they need to. Android系统的构建目的是使人们可以编写能够很好地完成一项特定任务的应用程序,而其他开发人员可以在需要时使用它们。 Barcode scanning is a great example. 条形码扫描就是一个很好的例子。 ZXing makes a great scanner and lets other apps use it via Intents. ZXing是一款出色的扫描仪,并允许其他应用通过Intents使用它。 Basically you tell the OS that you want to scan a barcode and ZXing says, "Yes, I can do that!" 基本上,您告诉操作系统您要扫描条形码,然后ZXing说:“是的,我可以做到!” They scan the barcode and return the information to you. 他们扫描条形码并将信息返回给您。 The great thing about doing it this way is that you don't have to worry about when they update their stuff. 这样做的好处是,您不必担心他们何时更新自己的东西。 The user just gets notified that it's updated and you get to use the latest and greatest. 只是通知用户它已更新,您就可以使用最新的和最大的更新。 The one potential downside is that the user has another app on their phone but I don't really see that as a draw back. 一个潜在的弊端是用户的手机上还有另一个应用程序,但我并不认为这是一个缺点。 To do it this way you really only need the two files you linked to and then you just place this in your code To start the scan: 为此,您实际上只需要链接到的两个文件,然后将其放置在代码中即可开始扫描:

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();

And this bit gets the answer from the barcode scanner: 这一点从条形码扫描仪得到答案:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanResult != null) {
        // handle scan result
    }
    // else continue with any other code you need in the method
  ...
}

Your only other option is to pull down all of the code for the barcode scanner and lump that into your project and then figure out how it all works and where you need to tie in to bring it into your app. 您唯一的其他选择是下拉条形码扫描器的所有代码,并将其合并到您的项目中,然后确定其工作方式以及需要结合到哪里才能将其引入应用程序。 Then you'll have to do that all over again every time ZXing makes an update. 然后,每次ZXing进行更新时,您都必须重新做一次。 It's a mess! 一团糟!

    You can use like this.It works in Activity as well as Fragment.
    Add this code in click listner of any view:


     scan.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  IntentIntegrator integrator = new IntentIntegrator(getActivity());


       integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                  integrator.setCameraId(0);
                  integrator.setBeepEnabled(true);
                  integrator.setBarcodeImageEnabled(true);
                  integrator.forFragment(InsuranceInfo.this).initiateScan();
                  System.out.println("CLick");
              }




On the Acivity Result Method  add this:-
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        System.out.println((intentResult.getContents()+""));

        super.onActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            //passing result to another Activity.
            //    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentResult.getContents() + ""));
         //   Toast.makeText(getActivity(),"Scan Results="+(intentResult.getContents()+""),Toast.LENGTH_SHORT).show();
            System.out.println((intentResult.getContents()+""));
            Snackbar.make(getActivity().findViewById(android.R.id.content),
                    ("Scan Results="+ (intentResult.getContents()+"")), Snackbar.LENGTH_LONG).show();
         ScanResult= (intentResult.getContents()+"");
         insurance.setText(ScanResult);

        }
        else {
            Toast.makeText(getActivity(), " Empty Result ", Toast.LENGTH_SHORT).show();
        }
    }

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

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