mitoyarzun / zxingfragmentlib Goto Github PK
View Code? Open in Web Editor NEWBarcode scanner for Android fragments. Compatible with Android 2.1+.
License: Apache License 2.0
Barcode scanner for Android fragments. Compatible with Android 2.1+.
License: Apache License 2.0
Hi Friends,
The reason why I write you is my wish to have the zxing fragment library expanded with a flashlight function.Below you may see my code that generates a flashlight.
My problem is now that I need a camera opener function.
Unfortunately the code doesn´t work due to the fact that the camera is already open. And so there is a conflict meaning that the camera can´t open twice, because the first camera-open-function was used for opening the bar code scanner, and so I get an error message when I try to open the camera the second time.
Now, how do I handle this situation?
Is it possible through the library to get access to switch on and put off function for the flashlight. Will you be kind enough to help me?
Exceptions:
E/Camera Error. Failed to Open. Error:(28639): Fail to connect to camera service
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.(Camera.java:413)
at android.hardware.Camera.open(Camera.java:384)
at dk.logoslogit.repository.ZxingScanFragment.getCamera(ZxingScanFragment.java:225)
at dk.logoslogit.repository.ZxingScanFragment.access$1(ZxingScanFragment.java:222)
at dk.logoslogit.repository.ZxingScanFragment$2.onClick(ZxingScanFragment.java:79)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
public class ZxingScanFragment extends Fragment {
private final String TAG = "ZxingScanFragment";
private FragmentTransaction fTransaction;
private Camera camera;
private Fragment fDisplay;
private ImageButton torch;
private boolean isFlashOn;
private boolean hasFlash;
private Parameters parameter;
private MediaPlayer player;
private View mView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
super.onCreateView(inflater, container, savedInstance);
mView = inflater.inflate(R.layout.scan_zxing, container, false);
setListTitle();
torch = (ImageButton) mView.findViewById(R.id.torch);
// First check if device is supporting flashlight or not
hasFlash = getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// Device doesn't support flash
// Show alert message and close the application
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Error");
alertDialog.setMessage("Sorry, your device doesn't support flash light!");
alertDialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Closing the application
dialog.cancel();
}
});
alertDialog.show();
}
// Displaying button image
toggleButtonImage();
// Switch button click event to toggle flash on/off
torch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
// On starting the app get the camera params
getCamera();
// Turn off flash
turnOffFlash();
} else {
// On starting the app get the camera params
getCamera();
// Turn on flash
turnOnFlash();
}
}
});
if (savedInstance == null) {
fDisplay = new ScanFragment();
fTransaction = getChildFragmentManager().beginTransaction();
fTransaction.remove(fDisplay);
fTransaction.replace(R.id.scan_layout, fDisplay);
fTransaction.commit();
}
return mView;
}
private void setListTitle() {
try {
TextView onlineStatus = (TextView) mView.findViewById(R.id.list_title);
onlineStatus.setText("Scan stregkode");
} catch (Exception ex) {
Log.v(TAG, "Exception: setListTitle");
ex.printStackTrace();
}
}
// Turning On flash
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || parameter == null) {
return;
}
// Play sound
playSound();
parameter = camera.getParameters();
parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameter);
camera.startPreview();
isFlashOn = true;
// Changing button/switch image
toggleButtonImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || parameter == null) {
return;
}
// Play sound
playSound();
parameter = camera.getParameters();
parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameter);
camera.stopPreview();
isFlashOn = false;
// Changing button/switch image
toggleButtonImage();
}
}
// Playing sound
// will play button toggle sound on flash on / off
private void playSound(){
if(isFlashOn) {
player = MediaPlayer.create(getActivity(), R.raw.light_switch_off);
} else {
player = MediaPlayer.create(getActivity(), R.raw.light_switch_on);
}
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
player.release();
}
});
player.start();
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage(){
if(isFlashOn) {
torch.setImageResource(R.drawable.scan_icon);
} else {
torch.setImageResource(R.drawable.room_icon);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
@Override
public void onPause() {
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
// On pause turn off the flash
turnOffFlash();
}
@Override
public void onResume() {
super.onResume();
// On resume turn off the flash
if(hasFlash) {
turnOffFlash();
}
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
// On stop release the camera
if (camera != null) {
camera.release();
camera = null;
}
}
// Get the camera
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
parameter = camera.getParameters();
} catch (RuntimeException re) {
Log.e("Camera Error. Failed to Open. Error: ", re.getMessage());
re.printStackTrace();
} catch (Exception ex) {
Log.e(TAG, "Exception: getCamera");
Log.e(TAG, ex.getMessage());
ex.printStackTrace();
}
}
}
When I rotate my phone to landscape orientation I get the following exception:
06-18 10:18:21.798 3193-3344/com.welcu.android.zxingfragmentlibsample E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-10361
Process: com.welcu.android.zxingfragmentlibsample, PID: 3193
java.lang.IllegalArgumentException: Crop rectangle does not fit within image data.
at com.google.zxing.PlanarYUVLuminanceSource.(PlanarYUVLuminanceSource.java:50)
at com.welcu.android.zxingfragmentlib.camera.CameraManager.buildLuminanceSource(CameraManager.java:360)
at com.welcu.android.zxingfragmentlib.DecodeHandler.decode(DecodeHandler.java:77)
at com.welcu.android.zxingfragmentlib.DecodeHandler.handleMessage(DecodeHandler.java:56)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at com.welcu.android.zxingfragmentlib.DecodeThread.run(DecodeThread.java:101)
Hello,
I have problem with scanning barcode with Samsung Galaxy S8 (dreamlte) with android 7.0. Screen is blurred while scanning.
Exception is:
java.lang.IllegalArgumentException:
at com.google.zxing.PlanarYUVLuminanceSource. (PlanarYUVLuminanceSource.java:50)
at com.google.zxing.client.android.camera.CameraManager.buildLuminanceSource (CameraManager.java:441)
at com.welcu.android.zxingfragmentlib.DecodeHandler.decode (DecodeHandler.java:87)
at com.welcu.android.zxingfragmentlib.DecodeHandler.handleMessage (DecodeHandler.java:53)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at com.welcu.android.zxingfragmentlib.DecodeThread.run (DecodeThread.java:96)
Scanning with other devices works fine.
Does someone have idea, where could be a problem?
Thanks a lot.
Michaela
Trying to run the code from Github facing the following issue.
Error:(32, 13) Failed to resolve: com.google.zxing:core:3.1.0
Show in File
Show in Project Structure dialog
Hi,
thanks for this great library!
I am using it as a git submodule in my current project and I have noticed that there is a small Lint error which unfortunately breaks my Jenkins build.
In CameraConfigurationManager, lines 142-147, Lint complains that the method Camera.setDisplayOrientation() is only available on Android 8 and above while the build.gradle states that the library works with 7 as well. In fact, you do the correct thing and the statement will only be executed on versions other than 7. However, Lint does not seem to recognize that "other than 7" is the same as "higher than 7" because of the minSdk specified in build.gradle. A simple "if (Build.VERSION.SDK_INT<=7) {" instead of "if (Build.VERSION.SDK_INT==7) {" fixes the problem.
I would appreciate it if you could incorporate this little fix because I want to stick with using your library and to performing Lint checks on the build server. ;-)
Thanks in advance!
Hi friends,
I found a bug in the zxing scanner. I use zxingfragmentlib to implement my fragment scanner. I have set android:screenOrientation to "portrait" in AndroidManifest, so I can only use it in portrait orientation.
I have tested the scanner in many android devices. I´ve found out that when I use the devise where the camera width is bigger than height then it rotates the camera view to 90 dregge.
Right now I have to rotate my device to 90 dregge in order to scan the barcode. It is not a good idea to do it that way.
I google the probleme for finding a solution. I found this page:
http://stackoverflow.com/questions/16252791/zxing-camera-in-portrait-mode-on-android
When I do what the Roylee says, the camera returns to the correct position but the scanner can't scan.
What should I do?
Please help me to figure out my problem
I thank you in advance.
Hi,
Using sample code I tested in my Nexus 4 and another datacollector and issue happens. No scan results when in portrait.
Thanks
i need a small frame,so i change max frame size.i can't get result if i near the camera .
The torch won't turn on by itself. Are the onKeyDown() and onKeyup() overriding the functions of AmbientLightManager?
Please help.
I have problem when set my SurfaceView with fix size of 300,300. The displaying image is distorted.
It's not a ZXing bug because the original app does not support portrait mode at all.
We should figure out a way to fix this.
Hi, a real nice feature would be an advanced history handling.
After a barcode is scanned, it would be great to have a notification, if the new scanned barcode is a new one or it already exist in the history. A display on the screen or a sound would be possible options.
thanks,
Hi
How can I use the front camera? Or is there any way to switch camera in this library?
Thank you
Hi,
I updated the library yesterday and, i got this issue...
java.lang.IllegalArgumentException: Crop rectangle does not fit within image data.
at com.google.zxing.PlanarYUVLuminanceSource.(PlanarYUVLuminanceSource.java:50)
at com.welcu.android.zxingfragmentlib.camera.CameraManager.buildLuminanceSource(CameraManager.java:360)
at com.welcu.android.zxingfragmentlib.DecodeHandler.decode(DecodeHandler.java:77)
at com.welcu.android.zxingfragmentlib.DecodeHandler.handleMessage(DecodeHandler.java:56)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at com.welcu.android.zxingfragmentlib.DecodeThread.run(DecodeThread.java:101)
It appears when I use the phone in landscape and open the my BarcodeScannerFragment.
I googled this error and, obviously, it means the device's camera driver has a bug.
BUT, i got the issue on multiple devices... and I don't really how to fix it...
Any clue ? Thanks
Laurent
Great work for porting this to use fragments, thanks!
I have a 3 tab viewpager. The barcode is fragment item number 2.
I start my application and the default fragment item 0 is shown. When I swipe to fragment item 1 the barcode scanner stored as item number 2 starts up, maybe the viewpager is caching it I'm not sure. This causes a bit of processing to occur as the camera initializes in the background and the DecodeThread starts up. Now this is all happening as fragment item 1 is showing not the barcode scanner which is fragment item 2.
Due to this there is a moment of about 1 second or ~60 frames frames skipped while switching between fragment item 0 and 1.
Here is an extract of my logcat :
04-23 23:13:59.076: I/CameraConfiguration(21035): Settable value: off
04-23 23:13:59.076: I/CameraConfiguration(21035): Supported values: [infinity, auto, macro, continuous-video, continuous-picture]
04-23 23:13:59.076: I/CameraConfiguration(21035): Settable value: auto
04-23 23:13:59.076: I/DecodeThread(21035): Hints: {POSSIBLE_FORMATS=[CODABAR, CODE_39, CODE_93, CODE_128, DATA_MATRIX, EAN_8, EAN_13, ITF, QR_CODE, RSS_14, RSS_EXPANDED, UPC_A, UPC_E], NEED_RESULT_POINT_CALLBACK=com.welcu.android.zxingfragmentlib.ViewfinderResultPointCallback@430cc730}
04-23 23:13:59.136: I/PlatformSupportManager(21035): Using implementation class com.welcu.android.zxingfragmentlib.common.executor.HoneycombAsyncTaskExecInterface of interface com.welcu.android.zxingfragmentlib.common.executor.AsyncTaskExecInterface for SDK 11
04-23 23:13:59.136: I/AutoFocusManager(21035): Current focus mode 'auto'; use auto focus? true
04-23 23:14:00.006: E/MediaPlayer(21035): Should have subtitle controller already set
04-23 23:14:00.006: I/Choreographer(21035): Skipped 63 frames! The application may be doing too much work on its main thread.
04-23 23:14:00.536: W/MediaPlayer-JNI(21035): MediaPlayer finalized without being released
Do you know of a way to keep the barcode scanner fragment from continually stopping and starting when swiping though fragments please?
Many Thanks,
Steve
It would be great if you could support new gradle version and target to new Android SDK 21.
Are you still developing this?
Thanks!
Autofocus runs fine while the flashlight is left off. once flashlight is turned on, autofocus stops. this persists even when flashlight is turned off.
tested with sample code and also implemented the code below in my app:
torchState = !torchState;
mScannerFragment.setTorch(torchState);
device : Samsung Galaxy S4 Active (LTE)
OS : Lollipop
Root : no
Hi there.
First of all, thank you for this awesome library.
I use it in my company and it works perfectly.
The only issue we have is that it's missing a license.
Would you be able to add one?
Thanks a lot!
Hi friends,
My problem is when i call the onCreateView from my fragment (Scan) the program crashes because i can't use it with a normal Fragment. I have to use FragmentActivity to use sample_activity.xml but thats the problem.
android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Is there any alternative i can use without using FragmentActivity?
I have successfully added the library to my project and it works as expected. One thing that I'm facing is that when I initiate the barcode scanner, the whole application goes blank for couple of seconds and then the camera shows up. The following times, when the camera is initiated the application doesn't go blank. Have anyone faced a similar issue? If yes what was your solution? Thanks!
sometimes, Scan the qr code seems like must be in a distance . if too closer, it doesn't work.
when scan the one dimension code, it donesn't work as qr code effectively.
but when i use the barcode scanner app, it always work well whether one code or qr code.
what can i do for this problem? Thank you for
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.