mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Added small android-integration module
git-svn-id: https://zxing.googlecode.com/svn/trunk@860 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
1e96791817
commit
81ef8e251c
1
AUTHORS
1
AUTHORS
|
@ -7,6 +7,7 @@ Brian Brown (Google)
|
||||||
Christian Brunschen (Google)
|
Christian Brunschen (Google)
|
||||||
Daniel Switkin (Google)
|
Daniel Switkin (Google)
|
||||||
David Albert (Bug Labs)
|
David Albert (Bug Labs)
|
||||||
|
Fred Lin (Anobiit)
|
||||||
John Connolly (Bug Labs)
|
John Connolly (Bug Labs)
|
||||||
Joseph Wain (Google)
|
Joseph Wain (Google)
|
||||||
Kevin O'Sullivan (SITA)
|
Kevin O'Sullivan (SITA)
|
||||||
|
|
48
android-integration/build.xml
Normal file
48
android-integration/build.xml
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Copyright 2009 ZXing authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project name="android-integration" default="build">
|
||||||
|
|
||||||
|
<property file="../build.properties"/>
|
||||||
|
|
||||||
|
<target name="init">
|
||||||
|
<tstamp/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="build" depends="init">
|
||||||
|
<mkdir dir="build"/>
|
||||||
|
<javac srcdir="src"
|
||||||
|
destdir="build"
|
||||||
|
source="1.5"
|
||||||
|
target="1.5"
|
||||||
|
optimize="true"
|
||||||
|
debug="true"
|
||||||
|
deprecation="true">
|
||||||
|
<classpath>
|
||||||
|
<pathelement location="${android-home}/android.jar"/>
|
||||||
|
</classpath>
|
||||||
|
</javac>
|
||||||
|
<jar jarfile="android-integration.jar" basedir="build"/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="clean">
|
||||||
|
<delete dir="build"/>
|
||||||
|
<delete file="android-integration.jar"/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,165 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2009 ZXing authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.integration.android;
|
||||||
|
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.ActivityNotFoundException;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>A utility class which helps ease integration with Barcode Scanner via {@link Intent}s. This is a simple
|
||||||
|
* way to invoke barcode scanning and receive the result, without any need to integrate, modify, or learn the
|
||||||
|
* project's source code.</p>
|
||||||
|
*
|
||||||
|
* <p>Integration is essentially as easy as calling {@link #initiateScan(Activity)} and waiting
|
||||||
|
* for the result in your app.</p>
|
||||||
|
*
|
||||||
|
* <p>It does require that the Barcode Scanner application is installed. The
|
||||||
|
* {@link #initiateScan(Activity)} method will prompt the user to download the application, if needed.</p>
|
||||||
|
*
|
||||||
|
* <p>There are a few steps to using this integration. First, your {@link Activity} must implement
|
||||||
|
* the method {@link Activity#onActivityResult(int, int, Intent)} and include a line of code like this:</p>
|
||||||
|
*
|
||||||
|
* <p>{@code
|
||||||
|
* public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||||
|
* IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
|
||||||
|
* if (scanResult != null) {
|
||||||
|
* // handle scan result
|
||||||
|
* }
|
||||||
|
* // else continue with any other code you need in the method
|
||||||
|
* ...
|
||||||
|
* }
|
||||||
|
* }</p>
|
||||||
|
*
|
||||||
|
* <p>This is where you will handle a scan result.
|
||||||
|
* Second, just call this in response to a user action somewhere to begin the scan process:</p>
|
||||||
|
*
|
||||||
|
* <p>{@code integrator.initiateScan();}</p>
|
||||||
|
*
|
||||||
|
* <p>You can use {@link #initiateScan(Activity, String, String, String, String)} or
|
||||||
|
* {@link #initiateScan(Activity, int, int, int, int)} to customize the download prompt with
|
||||||
|
* different text labels.</p>
|
||||||
|
*
|
||||||
|
* <p>Some code, particularly download integration, was contributed from the Anobiit application.</p>
|
||||||
|
*
|
||||||
|
* @author Sean Owen
|
||||||
|
* @author Fred Lin
|
||||||
|
*/
|
||||||
|
public final class IntentIntegrator {
|
||||||
|
|
||||||
|
public static final int REQUEST_CODE = 0x0ba7c0de; // get it?
|
||||||
|
|
||||||
|
private IntentIntegrator() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link #initiateScan(Activity, String, String, String, String)} -- same, but uses default English labels.
|
||||||
|
*/
|
||||||
|
public static void initiateScan(Activity activity) {
|
||||||
|
initiateScan(activity,
|
||||||
|
"Install Barcode Scanner?",
|
||||||
|
"This application requires Barcode Scanner. Would you like to install it?",
|
||||||
|
"Yes",
|
||||||
|
"No");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link #initiateScan(Activity, String, String, String, String)} -- same, but takes string IDs which refer
|
||||||
|
* to the {@link Activity}'s resource bundle entries.
|
||||||
|
*/
|
||||||
|
public static void initiateScan(Activity activity,
|
||||||
|
int stringTitle,
|
||||||
|
int stringMessage,
|
||||||
|
int stringButtonYes,
|
||||||
|
int stringButtonNo) {
|
||||||
|
initiateScan(activity,
|
||||||
|
activity.getString(stringTitle),
|
||||||
|
activity.getString(stringMessage),
|
||||||
|
activity.getString(stringButtonYes),
|
||||||
|
activity.getString(stringButtonNo));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invokes scanning.
|
||||||
|
*
|
||||||
|
* @param stringTitle title of dialog prompting user to download Barcode Scanner
|
||||||
|
* @param stringMessage text of dialog prompting user to download Barcode Scanner
|
||||||
|
* @param stringButtonYes text of button user clicks when agreeing to download Barcode Scanner (e.g. "Yes")
|
||||||
|
* @param stringButtonNo text of button user clicks when declining to download Barcode Scanner (e.g. "No")
|
||||||
|
* @return the contents of the barcode that was scanned, or null if none was found
|
||||||
|
* @throws InterruptedException if timeout expires before a scan completes
|
||||||
|
*/
|
||||||
|
public static void initiateScan(Activity activity,
|
||||||
|
String stringTitle,
|
||||||
|
String stringMessage,
|
||||||
|
String stringButtonYes,
|
||||||
|
String stringButtonNo) {
|
||||||
|
Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
|
||||||
|
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
|
||||||
|
try {
|
||||||
|
activity.startActivityForResult(intentScan, REQUEST_CODE);
|
||||||
|
} catch (ActivityNotFoundException e) {
|
||||||
|
showDownloadDialog(activity, stringTitle, stringMessage, stringButtonYes, stringButtonNo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void showDownloadDialog(final Activity activity,
|
||||||
|
String stringTitle,
|
||||||
|
String stringMessage,
|
||||||
|
String stringButtonYes,
|
||||||
|
String stringButtonNo) {
|
||||||
|
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
|
||||||
|
downloadDialog.setTitle(stringTitle);
|
||||||
|
downloadDialog.setMessage(stringMessage);
|
||||||
|
downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
|
Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
|
||||||
|
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
|
||||||
|
activity.startActivity(intent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(DialogInterface dialogInterface, int i) {}
|
||||||
|
});
|
||||||
|
downloadDialog.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Call this from your {@link Activity}'s {@link Activity#onActivityResult(int, int, Intent)} method.</p>
|
||||||
|
*
|
||||||
|
* @return null if the event handled here was not related to {@link IntentIntegrator}, or
|
||||||
|
* else an {@link IntentResult} containing the result of the scan. If the user cancelled scanning,
|
||||||
|
* the fields will be null.
|
||||||
|
*/
|
||||||
|
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");
|
||||||
|
return new IntentResult(contents, formatName);
|
||||||
|
} else {
|
||||||
|
return new IntentResult(null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2009 ZXing authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.integration.android;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Encapsulates the result of a barcode scan invoked through {@link IntentIntegrator}.</p>
|
||||||
|
*
|
||||||
|
* @author Sean Owen
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue