Using Zxing and Google Goggles with my app
Categories:
Integrating Barcode Scanning and OCR with ZXing and Google Goggles in Android

Learn how to leverage the power of ZXing for robust barcode and QR code scanning, and explore options for integrating OCR capabilities, including the now-deprecated Google Goggles API, into your Android applications.
Integrating barcode scanning and Optical Character Recognition (OCR) into mobile applications can significantly enhance user experience and data capture efficiency. This article focuses on using the popular ZXing library for barcode and QR code scanning in Android, and discusses the historical context and alternatives for OCR, particularly in light of Google Goggles' API deprecation. While Google Goggles is no longer directly available for API integration, understanding its former capabilities helps in exploring modern OCR solutions.
ZXing: The Go-To for Barcode and QR Code Scanning
ZXing (pronounced "zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. It's widely used in Android development for its robust and efficient barcode scanning capabilities. Integrating ZXing typically involves either embedding the library directly into your project or invoking the ZXing Barcode Scanner app via an Intent.
flowchart TD A[Your Android App] --> B{"Launch Scanner Intent?"} B -- Yes --> C[ZXing Barcode Scanner App] C --> D[Scan Barcode/QR Code] D --> E[Return Result to Your App] B -- No --> F[Embed ZXing Library] F --> G[Implement Custom Scanner UI] G --> H[Process Scanned Image] H --> E
ZXing Integration Flowchart: Intent vs. Embedded Library
Implementing ZXing via Intent
The easiest way to integrate ZXing is to use the IntentIntegrator
class from the zxing-android-embedded
library. This library provides a convenient wrapper around the standard Android Intent mechanism, allowing you to easily launch the Barcode Scanner app and receive its results.
import com.journeyapps.barcodescanner.CaptureActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.scan_button).setOnClickListener(v -> {
new IntentIntegrator(this).initiateScan();
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
// Process the scanned content here
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Example of launching ZXing scanner and handling results in an Android Activity.
OCR with Google Goggles (Historical Context) and Modern Alternatives
Google Goggles was an early mobile app that allowed users to search by taking a picture. It included OCR capabilities, enabling it to read text from images. While its direct API for developers was deprecated, its underlying technology paved the way for more advanced OCR solutions. Today, for robust OCR in Android, developers typically turn to Google's ML Kit or third-party libraries.
flowchart TD A[User Takes Photo] --> B{Image Processing} B --> C[Text Detection (ML Kit)] C --> D[Text Recognition (ML Kit)] D --> E[Extracted Text] E --> F[Your App Logic]
Modern OCR Workflow using Google ML Kit
Implementing OCR with Google ML Kit
Google ML Kit provides powerful on-device and cloud-based text recognition capabilities. The on-device API is fast and works offline, making it suitable for many applications. Integrating it involves adding the ML Kit dependency, preparing an image, and passing it to the TextRecognizer
.
import com.google.mlkit.vision.common.InputImage;
import com.google.mlkit.vision.text.TextRecognition;
import com.google.mlkit.vision.text.TextRecognizer;
import com.google.mlkit.vision.text.latin.TextRecognizerOptions;
import android.graphics.Bitmap;
import android.widget.Toast;
public class OcrProcessor {
public interface OcrResultListener {
void onTextRecognized(String recognizedText);
void onError(Exception e);
}
public void recognizeTextFromImage(Bitmap bitmap, OcrResultListener listener) {
InputImage image = InputImage.fromBitmap(bitmap, 0);
TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
recognizer.process(image)
.addOnSuccessListener(visionText -> {
listener.onTextRecognized(visionText.getText());
})
.addOnFailureListener(e -> {
listener.onError(e);
});
}
}
Basic OCR implementation using Google ML Kit's Text Recognition API.