Android Zxing Embedded Barcodeview Not Resuming
In my android app I use a Fragment (CameraFragment see below) which uses a BarcodeView. When a Barcode is scanned(BarcodeCallBack) I check if it fits a pattern and if not I want to
Solution 1:
Hi user1406731 you are using the wrong callback, we do something similar in our mobile app. Replace your decodeSingle() callback function with decodeContinuous() callback and it should work. Here is a working example from the official doc Continous Callback example. Hope it helps you.
Hi i tried to run your code & made a little change. This is what i tested and works fine for me
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.BarcodeView;
import com.example.scanner.R;
import com.example.scanner.ui.base.BaseFragment;
import java.util.List;
/**
* Created by Rishabh Bhatia on 12/5/17.
*/publicclassCameraFragmentextendsBaseFragment {
Context mcontext;
privateBarcodeView barcodeView;
final int PERMISSIONS_REQUEST_ACCESS_CAMERA=0;
View view;
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_camera, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
getActivity().checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(newString[]{Manifest.permission.CAMERA},
PERMISSIONS_REQUEST_ACCESS_CAMERA);
} else {
barcodeView = (BarcodeView) view.findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
}
mcontext=getContext();
return view;
}
@OverridepublicvoidonResume() {
super.onResume();
barcodeView = (BarcodeView) view.findViewById(R.id.barcode_scanner);
barcodeView.resume();
}
@OverridepublicvoidonPause() {
super.onPause();
barcodeView = (BarcodeView) view.findViewById(R.id.barcode_scanner);
barcodeView.pause();
}
privateBarcodeCallback callback = newBarcodeCallback() {
@OverridepublicvoidbarcodeResult(BarcodeResult result) {
if (result.getText() != null) {
barcodeView.pause();
//after the string has been read we prozess itString tag_string=result.getText();
Log.e("RISHABH", "pausing scanner, got some data "+tag_string);
if(!tag_string.equals("abc")){//if the tag was not scanned succesfully let us start the scan againLog.e("RISHABH", "resuming scanner, data needed was not found");
barcodeView.resume(); //notice we don't call decodeContinuous function again
}
}
}
@OverridepublicvoidpossibleResultPoints(List<ResultPoint> resultPoints) {
}
};
@OverridepublicvoidonRequestPermissionsResult(int requestCode, @NonNullString[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_ACCESS_CAMERA) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
barcodeView = (BarcodeView) view.findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
}
}
}
}
Here is my logcat
05-12 23:54:13.226 31403-31403/com.example.scanner E/RISHABH: pausing scanner, got some data ABC-abc-1234
05-12 23:54:13.226 31403-31403/com.example.scanner E/RISHABH: resuming scanner, data needed was not found
05-12 23:54:18.280 31403-31403/com.example.scanner E/RISHABH: pausing scanner, got some data ABC-abc-1234
05-12 23:54:18.280 31403-31403/com.example.scanner E/RISHABH: resuming scanner, data needed was not found
05-12 23:54:19.350 31403-31403/com.example.scanner E/RISHABH: pausing scanner, got some data ABC-abc-1234
05-12 23:54:19.350 31403-31403/com.example.scanner E/RISHABH: resuming scanner, data needed was not found
05-12 23:54:20.350 31403-31403/com.example.scanner E/RISHABH: pausing scanner, got some data ABC-abc-1234
05-12 23:54:20.350 31403-31403/com.example.scanner E/RISHABH: resuming scanner, data needed was not found
05-12 23:54:21.450 31403-31403/com.example.scanner E/RISHABH: pausing scanner, got some data ABC-abc-1234
05-12 23:54:21.450 31403-31403/com.example.scanner E/RISHABH: resuming scanner, data needed was not found
05-12 23:54:22.510 31403-31403/com.example.scanner E/RISHABH: pausing scanner, got some data ABC-abc-1234
05-12 23:54:22.510 31403-31403/com.example.scanner E/RISHABH: resuming scanner, data needed was not found
05-12 23:54:33.000 31403-31403/com.example.scanner E/RISHABH: pausing scanner, got some data abc
Solution 2:
I figured it out and thought it might help someone. I had exactly the same issue, then figured out that resume() method do a check if it is running on MainThread.
publicclassUtil {
publicstaticvoidvalidateMainThread() {
if (Looper.getMainLooper() != Looper.myLooper()) {
thrownew IllegalStateException("Must be called from the main thread.");
}
}
}
Here are examples of how to run code on MainThread: Running code in main thread from another thread
I tried to run it from @JavscriptInterface method and here is my code:
publicclassMyInterface {
Context mContext;
ActivityMain activityMain;
MyInterface(Context c, ActivityMain am) {
mContext = c;
activityMain = am;
}
@JavascriptInterfacepublicvoidresumeScanner() {
Toast.makeText(mContext, "resume", Toast.LENGTH_SHORT).show();
HandlermainHandler=newHandler(mContext.getMainLooper());
RunnablemyRunnable=newRunnable() {
@Overridepublicvoidrun() {
activityMain.barcodeView.resume();
}
};
mainHandler.post(myRunnable);
}
}
Post a Comment for "Android Zxing Embedded Barcodeview Not Resuming"