Barcode Scanner sample
main.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" android:layout_gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center" android:textSize="25sp"/>
<Button android:text="Scan QR Code" android:id="@+id/scanner" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal"></Button>
<Button android:text="Scan BAR Code" android:id="@+id/scanner2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal"></Button>
</LinearLayout>
AndroidScanner.java
<?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" android:layout_gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center" android:textSize="25sp"/>
<Button android:text="Scan QR Code" android:id="@+id/scanner" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal"></Button>
<Button android:text="Scan BAR Code" android:id="@+id/scanner2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal"></Button>
</LinearLayout>
AndroidScanner.java
package com.as400samplecode; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class AndroidScanner extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.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(); } 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(); } } } }
Comments
Post a Comment