实现条形码阅读器教程

时间:2014-06-03 13:27:28

标签: android

我是Android开发的新手,想要为我正在开展的营销项目构建自己的QR扫描应用。我开始阅读并尝试教程,并找到了this BarcodeSanner实现的简单解释。我做了所有事情,但我的应用程序无法正常工作。当我在模拟器上运行它时,它表示应用程序已崩溃并关闭。有人可以帮助我并告诉我我做错了什么。我在Zxing的网站上使用了这个文件IntentIntegrator。这是我的MainActivity文件:

    package com.example.barcodesanningapp;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends  Activity implements OnClickListener {
    private Button scanBtn;
    private TextView formatTxt, contentTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scanBtn = (Button)findViewById(R.id.scan_button);
        formatTxt = (TextView)findViewById(R.id.scan_format);
        contentTxt = (TextView)findViewById(R.id.scan_content);
        scanBtn.setOnClickListener(this);
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.scan_button){
            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            scanIntegrator.initiateScan();
            }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanningResult != null) {
            String scanContent = scanningResult.getContents();
            String scanFormat = scanningResult.getFormatName();
            formatTxt.setText("FORMAT: " + scanFormat);
            contentTxt.setText("CONTENT: " + scanContent);
            }
        else{
            Toast toast = Toast.makeText(getApplicationContext(), 
                "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }

    }

}

此处还有IntentResult文件:

package com.google.zxing.integration.android;

public final class IntentResult {

private final String contents;
private final String formatName;

  IntentResult(String contents, String formatName) {
  this.contents = contents;
  this.formatName = formatName;
  }

  /**
   * @return raw content of barcode
  */
   public String getContents() {
   return contents;
 }

  /**
  * @return name of format, like "QR_CODE", "UPC_A". See <code>BarcodeFormat</code> for        more format names.
   */
  public String getFormatName() {
    return formatName;
  }

  }

我不得不在IntentIntegrator文件中做一个小改动,因为它在这个方法中给了我一个错误:

public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
  if (resultCode == Activity.RESULT_OK) {
    String contents = intent.getStringExtra("SCAN_RESULT");
    String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
    byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
    int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
    Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation;
    String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
    return new IntentResult(contents,
                            formatName,
                            rawBytes,
                            orientation,
                            errorCorrectionLevel);
  }
  return new IntentResult();
}
return null;

}

我把它更改为:

return new IntentResult(contents, formatName);
   }
   return new IntentResult(null, null);

我希望有人可以通过简单的方式向我解释,以便我能理解并修复它。谢谢你的帮助。

P.S 编辑:

以下是运行应用程序后LogCat的内容:

  

06-03 14:01:19.254:D / AndroidRuntime(1809):关闭VM 06-03   14:01:19.254:W / dalvikvm(1809):threadid = 1:线程退出   未捕获的异常(组= 0xb3033180)06-03 14:01:19.254:   E / AndroidRuntime(1809):致命异常:主06-03 14:01:19.254:   E / AndroidRuntime(1809):java.lang.RuntimeException:无法启动   活动   ComponentInfo {com.example.barcodesanningapp / com.example.barcodesanningapp.MainActivity}:   java.lang.NullPointerException 06-03 14:01:19.254:   E / AndroidRuntime(1809):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)   06-03 14:01:19.254:E / AndroidRuntime(1809):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)   06-03 14:01:19.254:E / AndroidRuntime(1809):at   android.app.ActivityThread.access $ 600(ActivityThread.java:123)06-03   14:01:19.254:E / AndroidRuntime(1809):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)   06-03 14:01:19.254:E / AndroidRuntime(1809):at   android.os.Handler.dispatchMessage(Handler.java:99)06-03   14:01:19.254:E / AndroidRuntime(1809):at   android.os.Looper.loop(Looper.java:137)06-03 14:01:19.254:   E / AndroidRuntime(1809):at   android.app.ActivityThread.main(ActivityThread.java:4424)06-03   14:01:19.254:E / AndroidRuntime(1809):at   java.lang.reflect.Method.invokeNative(Native Method)06-03   14:01:19.254:E / AndroidRuntime(1809):at   java.lang.reflect.Method.invoke(Method.java:511)06-03 14:01:19.254:   E / AndroidRuntime(1809):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)   06-03 14:01:19.254:E / AndroidRuntime(1809):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)06-03   14:01:19.254:E / AndroidRuntime(1809):at   dalvik.system.NativeStart.main(Native Method)06-03 14:01:19.254:   E / AndroidRuntime(1809):引起:java.lang.NullPointerException   06-03 14:01:19.254:E / AndroidRuntime(1809):at   com.example.barcodesanningapp.MainActivity.onCreate(MainActivity.java:34)   06-03 14:01:19.254:E / AndroidRuntime(1809):at   android.app.Activity.performCreate(Activity.java:4465)06-03   14:01:19.254:E / AndroidRuntime(1809):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)   06-03 14:01:19.254:E / AndroidRuntime(1809):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)   06-03 14:01:19.254:E / AndroidRuntime(1809):... 11更多06-03   14:01:19.693:I / dalvikvm(1809):threadid = 3:对信号3 06-03作出反应   14:01:19.693:I / dalvikvm(1809):写了堆栈跟踪   &#39; /data/anr/traces.txt' 06-03 14:01:19.975:I / dalvikvm(1809):   threadid = 3:对信号作出反应3 06-03 14:01:19.975:I / dalvikvm(1809):   将堆栈跟踪写入&#39; /data/anr/traces.txt'

1 个答案:

答案 0 :(得分:0)

我一直在使用相同的东西,它完美无缺。你可以发布启动scanIntegrator的完整活动吗? 发布strackTrace

默认情况下,模拟器没有摄像头,可能因为无法找到摄像头而崩溃。

问题如下: - 你的Activity实现了OnClickListener,我只能假设这是因为你想按一个按钮? - 还是scanBtn.setOnClickListener(this);返回null;您应该检查scanBtn是否为空。

这是将clicklistener添加到按钮的正确方法

scanBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            IntentIntegrator scanIntegrator = new IntentIntegrator(this);
            scanIntegrator.initiateScan();
        }
    });