java - 在 Android 应用程序中使用 zxing 扫描条形码阅读器数据

标签 java android barcode zxing barcode-scanner

这是我的代码不适合我

 public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
                startActivityForResult(intent, 0);
            }

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

            Toast.makeText(getApplicationContext(),"Contents"+contents+"Format"+format,Toast.LENGTH_LONG).show();
            tv.setText(contents+""+format);
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}

startActivityForResult(intent, 0) 出错;我不知道如何解决这个错误请帮助我

最佳答案

您也可以通过扩展 CaptureActivity 来实现。

 public void onClick(View v) {
   Intent intent=new Intent(A.this,ScannerActivity.class);
    startActivity(intent);
        }

扫描仪 Activity :

public class ScannerActivity extends CaptureActivity {

/** The barcode format. */
String barcodeFormat;

/**
 * Called when the activity is first created.
 * 
 * @param savedInstanceState
 *            the saved instance state
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.barcode_scan);
}

/*
 * (non-Javadoc)
 * 
 * @see
 * com.google.zxing.client.android.CaptureActivity#handleDecode(com.google
 * .zxing.Result, android.graphics.Bitmap)
 */
@Override
public void handleDecode(Result rawResult, Bitmap barcode) {

    String barcodeType = rawResult.getBarcodeFormat().toString();
    String productId = rawResult.getText();
 Toast.makeText(getApplicationContext(),"Contents"+productId +"Format"+barcodeType ,Toast.LENGTH_LONG).show();
    //handle intent by sending product id as your wish...

}

barcode_scan.xml:(不要忘记包含 capture.xml)

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
   android:orientation="vertical" >
    <include layout="@layout/capture"/> 
</LinearLayout>

关于java - 在 Android 应用程序中使用 zxing 扫描条形码阅读器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29096453/

相关文章:

java - 获取选项卡式 Activity 的当前选定选项卡

java.lang.IllegalArgumentException : Parameter does not exist as a named parameter in 异常

android - 当复选框使用 setEnable(true) 时更改 .setTextColor 的颜色

Android HttpResponse - 内容已被消费

java - 在 Fragment 中使用 CustomView 发生 ClassCastException

java - 如果所有消费者都以相同的方式使用相同的 API,为什么我们应该为每个消费者准备 1 个 Pact 文件?

android - 嵌入式 ZXing - 我缺少什么?

java - 如何在 Java/iReport 中旋转条形码?

barcode - 如何检测 TIFF 图像中的多个条形码/二维码并返回它们的值 + 位置?

java - 为什么substring()方法是substring(起始索引(含),结束索引(不含))