簡體   English   中英

在Android應用程序中使用zxing掃描條形碼閱讀器數據

[英]scan the barcode reader data using zxing in android app

這是我的代碼對我不起作用

 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); 我不知道怎么能解決這個錯誤PLZ幫助我

您也可以通過擴展CaptureActivity來完成。

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

ScannerActivity:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM