使用ZXING获取扫描内容 - Android

时间:2015-04-22 06:47:17

标签: android zxing

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            Button scanner = (Button) findViewById(R.id.scanner);
            scanner.setOnClickListener(new OnClickListener() {

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

            });

            Button scanner2 = (Button) findViewById(R.id.scanner2);
            scanner2.setOnClickListener(new OnClickListener() {

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

            });

        } catch (ActivityNotFoundException anfe) {
            Log.e("onCreate", "Scanner Not Found", anfe);
        }

    }

    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");

                // Handle successful scan
                // Toast toast = Toast.makeText(this, "Content:" + contents
                // + " Format:" + format, Toast.LENGTH_LONG);
                // toast.setGravity(Gravity.TOP, 25, 400);
                // toast.show();

                Toast.makeText(getApplicationContext(), contents, 100).show();
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
                Toast toast = Toast.makeText(this, "Scan was Cancelled!",
                        Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400);
                toast.show();

            }
        }
    }

}

这是我的代码我正在扫描二维码使用Zixing库我能够扫描但问题无法在我的动作类中获得价值我想要在扫描成功完成后它应该打印Toast并且scaing actvity应该完成。请帮我解决这个问题。

下面的

是我的清单文件:

<activity
            android:name="com.example.zxing.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
               <action android:name="com.google.zxing.client.android.SCAN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

有CaptureActivity我只想从谷歌Zxing librabry这个最大的文件中得到帮助。

2 个答案:

答案 0 :(得分:1)

onActivityResult您需要致电

IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

示例用法

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (result != null) {
      String contents = result.getContents();
      if (contents != null) {
        Log.d(TAG, "Success");
        // result.toString() holds the result info
      } else {
        Log.d(TAG, "Failed");
      }
    }
  }

<强>更新 在扫描按钮上单击执行以下操作

Button scanner = (Button) findViewById(R.id.scanner);
scanner.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
      IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
      integrator.addExtra("SCAN_WIDTH", 800);
      integrator.addExtra("SCAN_HEIGHT", 200);
      integrator.addExtra("RESULT_DISPLAY_DURATION_MS", 3000L);
      integrator.addExtra("PROMPT_MESSAGE", "Custom prompt to scan a product");
      integrator.initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES);

     }

});

对于 QR-CODE 扫描,请执行此操作

IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);

关注test了解详情。

答案 1 :(得分:0)

您可以制作一些自定义功能

private void showMsg(String msg) {
    // TODO Auto-generated method stub
    final AlertDialog build = new AlertDialog.Builder(this).create();
    build.setTitle("Scanning Result");
    build.setMessage(msg);
    build.setCancelable(true);
    build.setButton(AlertDialog.BUTTON_POSITIVE, " OK ",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    build.dismiss();
                }
            });
    build.show();
}

然后调用函数而不是Toast。

showMsg("Product get with " + res);