I want my application to recognize barcodes taken by camera. Is it possible using Android SDK?
Something like this: Barcode Scanner
I want my application to recognize barcodes taken by camera. Is it possible using Android SDK?
Something like this: Barcode Scanner
SDK には組み込まれていませんが、Zxing ライブラリを使用できます。無料で、オープン ソースで、Apache ライセンスを取得しています。
2016 年の推奨事項は、オフラインでも動作するBarcode APIを使用することです。
カメラAPIを使ったサンプルコードはこちら
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
public class MainActivity extends AppCompatActivity {
TextView barcodeInfo;
SurfaceView cameraView;
CameraSource cameraSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraView = (SurfaceView) findViewById(R.id.camera_view);
barcodeInfo = (TextView) findViewById(R.id.txtContent);
BarcodeDetector barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.CODE_128)//QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
barcodeInfo.post(new Runnable() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText( // Update the TextView
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gateway.cameraapibarcode.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<SurfaceView
android:layout_width="640px"
android:layout_height="480px"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/camera_view"/>
<TextView
android:text=" code reader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtContent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Process"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgview"/>
</LinearLayout>
</RelativeLayout>
build.gradle(モジュール:アプリ)
依存関係にcompile 'com.google.android.gms:play-services:7.8.+'を追加
サンプル コードは次のとおりです。私のアプリは ZXing Barcode Scanner を使用しています。
次の 2 つのクラスが必要です: IntentIntegratorと IntentResult
スキャナーを呼び出します (例: OnClickListener、OnMenuItemSelected...)、"PRODUCT_MODE" - 標準の 1D バーコードをスキャンします (さらに追加できます)。
IntentIntegrator.initiateScan(this,
"Warning",
"ZXing Barcode Scanner is not installed, download?",
"Yes", "No",
"PRODUCT_MODE");
結果としてバーコードを取得します。
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
this.elemQuery.setText(contents);
this.resume = false;
Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
}
}
内容はバーコード番号を保持します
モジュールアプリ:
implementation 'com.google.zxing:core:3.2.1'
implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button BarCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BarCode = findViewById(R.id.button_barcode);
final Activity activity = this;
BarCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
intentIntegrator.setDesiredBarcodeFormats(intentIntegrator.ALL_CODE_TYPES);
intentIntegrator.setBeepEnabled(false);
intentIntegrator.setCameraId(0);
intentIntegrator.setPrompt("SCAN");
intentIntegrator.setBarcodeImageEnabled(false);
intentIntegrator.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){
Log.d("MainActivity" , "cancelled scan");
Toast.makeText(this, "cancelled", Toast.LENGTH_SHORT).show();
}
else {
Log.d("MainActivity" , "Scanned");
Toast.makeText(this,"Scanned -> " + Result.getContents(), Toast.LENGTH_SHORT).show();
}
}
else {
super.onActivityResult(requestCode , resultCode , data);
}
}
}
zxingの拡張であるbarcodefragmentlibを使用することもできますが、フラグメントライブラリとしてバーコードスキャンを提供するため、非常に簡単に統合できます。
ライブラリの使用に関するサポートドキュメントは次のとおりです
バーコード スキャン機能の構築には 2 つの部分があります。1 つはカメラを使用してバーコード イメージをキャプチャし、もう 1 つはイメージからバーコード値を抽出します。
カメラアプリを使用してアプリからバーコード画像をキャプチャし、Firebase Machine Learning Kit バーコード スキャン API を使用してバーコード値を抽出できます。
これがアプリの例です https://www.zoftino.com/android-barcode-scanning-example
parseActivityForResult 引数に問題がありました。私はこれを機能させました:
package JMA.BarCodeScanner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class JMABarcodeScannerActivity extends Activity {
Button captureButton;
TextView tvContents;
TextView tvFormat;
Activity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
captureButton = (Button)findViewById(R.id.capture);
captureButton.setOnClickListener(listener);
tvContents = (TextView)findViewById(R.id.tvContents);
tvFormat = (TextView)findViewById(R.id.tvFormat);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
switch (requestCode)
{
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK)
{
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null)
{
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
tvContents.setText(contents.toString());
tvFormat.setText(format.toString());
//this.elemQuery.setText(contents);
//this.resume = false;
Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
}
else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
}
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.initiateScan();
}
};
}
アクティビティのレイアウト:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take a Picture"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvContents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvFormat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>