Android Example QR Code Scanning View Finder Width and Height

SLC1.png


This is what I have done to be able to control the width/height of the scanning window in Nelson's project posted at http://www.b4x.com/android/forum/th...ode-ean-13-ean-8-generator.37447/#post-220973

1. I have set up a project in a folder named FirstLibrary and created sub folders "libs" and "src" to it. I then added files to these folders. The final layout looked like this:

FirstLibrary
libs
zxingscanner.jar
core-zxing.jar​
src
com
inforpires
barcodescanner
barcodescanner.java
The zxingscanner.jar comes from the libs.zip in Nelson posting at http://www.b4x.com/android/forum/th...ode-ean-13-ean-8-generator.37447/#post-220973. The core-zxing.jar file was downloaded from the web.

barcodescanner.java was generated from Nelson's code posted at http://www.b4x.com/android/forum/th...ode-ean-13-ean-8-generator.37447/#post-220973.

Nelson's original code:

B4X:
/*
       Based on the original work of IceFairy333

*/
package com.inforpires.barcodescanner;

import android.content.Intent;
import android.util.Log;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.IOnActivityResult;
import anywheresoftware.b4a.BA.DependsOn;

import com.google.zxing.client.android.*;


@ShortName("Zxing_scanner")
@Version(1.0F)
@Author("Nelson Pires")
@Events(values={"result(atype as String,Values as String)","noScan"})
@ActivityObject
@Permissions(values={"android.permission.CAMERA","android.permission.WRITE_EXTERNAL_STORAGE","android.hardware.camera","android.hardware.camera.autofocus","android.permission.VIBRATE","android.permission.FLASHLIGHT"})
@DependsOn(values={"core-zxing","zxingscanner"})


public class barcodescanner {
    @Hide
    public static BA myba;
    private IOnActivityResult ion;
    @Hide
    public static String en;

public void BeginScan(BA ba,String EventName, String msg) {
    myba=ba;
    en=EventName.toLowerCase();
    Intent izx;


    izx=new Intent(BA.applicationContext, CaptureActivity.class);
    izx.setAction("com.google.zxing.client.android.SCAN");
    izx.putExtra("PROMPT_MESSAGE",msg);

    ion=new IOnActivityResult() {

        @Override
        public void ResultArrived(int arg0, Intent arg1) {
            // TODO Auto-generated method stub
            if (arg0==-1){
            String atype;
            String Value;
            atype=arg1.getStringExtra("SCAN_RESULT_FORMAT");
            Value=arg1.getStringExtra("SCAN_RESULT");
            myba.raiseEvent2(null, false, en+"_result", true, new Object[]{atype,Value});
            Log.i("B4A", "Got result code:"+Value);
            }else{
                myba.raiseEvent(null,en+"_noscan");
                Log.i("B4A", "Got other result code:"+arg0);
            }
        }
    };
    ba.startActivityForResult(ion, izx);
}

}

I then amended the code as follows by making use of Notepad in Windows and saved it as barcodescanner.java in the directory as per the directory tree above:

Amended this:
public void BeginScan(BA ba,String EventName, String msg)
to
public void BeginScan(BA ba,String EventName, String msg, int scan_width, int scan_height)

and
added this:
izx.putExtra("SCAN_WIDTH",scan_width);
izx.putExtra("SCAN_HEIGHT",scan_height);


Nelson's modified/amended code:
B4X:
/*
       Based on the original work of IceFairy333

*/
package com.inforpires.barcodescanner;

import android.content.Intent;
import android.util.Log;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.IOnActivityResult;
import anywheresoftware.b4a.BA.DependsOn;

import com.google.zxing.client.android.*;


@ShortName("Zxing_scanner")
@Version(1.0F)
@Author("Nelson Pires")
@Events(values={"result(atype as String,Values as String)","noScan"})
@ActivityObject
@Permissions(values={"android.permission.CAMERA","android.permission.WRITE_EXTERNAL_STORAGE","android.hardware.camera","android.hardware.camera.autofocus","android.permission.VIBRATE","android.permission.FLASHLIGHT"})
@DependsOn(values={"core-zxing","zxingscanner"})


public class barcodescanner {
    @Hide
    public static BA myba;
    private IOnActivityResult ion;
    @Hide
    public static String en;

public void BeginScan(BA ba,String EventName, String msg, int scan_width, int scan_height) {
    myba=ba;
    en=EventName.toLowerCase();
    Intent izx;


    izx=new Intent(BA.applicationContext, CaptureActivity.class);
    izx.setAction("com.google.zxing.client.android.SCAN");
    izx.putExtra("PROMPT_MESSAGE",msg);
    izx.putExtra("SCAN_WIDTH",scan_width);
    izx.putExtra("SCAN_HEIGHT",scan_height);

    ion=new IOnActivityResult() {

        @Override
        public void ResultArrived(int arg0, Intent arg1) {
            // TODO Auto-generated method stub
            if (arg0==-1){
            String atype;
            String Value;
            atype=arg1.getStringExtra("SCAN_RESULT_FORMAT");
            Value=arg1.getStringExtra("SCAN_RESULT");
            myba.raiseEvent2(null, false, en+"_result", true, new Object[]{atype,Value});
            Log.i("B4A", "Got result code:"+Value);
            }else{
                myba.raiseEvent(null,en+"_noscan");
                Log.i("B4A", "Got other result code:"+arg0);
            }
        }
    };
    ba.startActivityForResult(ion, izx);
}

}

I then started Simple Library Compiler (http://www.b4x.com/android/forum/th...mpiler-build-libraries-without-eclipse.29918/) and set the required parameters as per the attached png file, clicked on button Compile and that was it as far as the compiling was concerned.

The library files Zxing_scanner.jar and Zxing_scanner.xml were generated and added to my additional libraries folder. I copied the two files to the B4A default library folder, opened Nelson's original project, refreshed the libraries, selected baQRCode and Zxing_scanner, amended and added code as per lines 66 to 70 of the project posted at http://www.b4x.com/android/forum/th...ode-ean-13-ean-8-generator.37447/#post-280091

.....and that was it. It worked! Hope this is useful form someone else.


Edit 14 Nov 2014
See attached project originally by Nelson et al. I have added the option to specify the width and height of the scan rectangle when the camera is active. Code in lines 66 to 69 added and code in line 70 amended to accommodate passing the width and height.

1. The library files were created as per above posting i.e with Simple Library Compiler
2. By adding izx.putExtra("SCAN_WIDTH", width) and izx.putExtra("SCAN_HEIGHT",height) and then compiling Nelson's wrapper the width and height can be passed from his B4A project to the zxing library according to the amendments that I have made in lines 66 to 70 of Nelson's project - thanks Nelson!
The library files that you require for this project should be in the /files folder of the attached project (i.e baqrcode.jar, baqrcode.xml, zxing_scanner.jar, and zxing_scanner.xml). Copy them to your default B4A libs folder, refresh the libs in the project (if need be) and select the two libs if they are not already selected.

If you want to change some colors (for eg the laser, the masking window around the viewfinder window, etc) you can do so by editing and changing the color values in /Objects/res/values/colors.xml of the attached project. Then recompile the B4A project for the color changes to take effect.

The scanning part of Project QRCP4 at http://www.b4x.com/android/forum/threads/qr-code-library.41408/#post-259448 is based on the project posted here.
 

Attachments

  • NELSON SOLUTION V2.zip
    188.8 KB · Views: 1,055
Last edited:

Reids

Member
Licensed User
Longtime User
Thank you, I followed recently your post here, but can you tell me how to make additional text into scanner window?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Thank you, I followed recently your post here, but can you tell me how to make additional text into scanner window?

The only way to change the text is by changing the code on line 70 of the project:

sc.BeginScan("sc","Modified by Johan Schoeman - see lines 66 to 69 of the code",scan_width,scan_height)

Change the text in RED to whatever you want it to be...

or something like this

sc.BeginScan("sc","Modified by Johan Schoeman - see lines 66 to 69 of the code" & Chr(10) & Chr(13) & "hallo"
 

Reids

Member
Licensed User
Longtime User
Yeah I know that text appear below scanner window, but I want to add additional text on specified position,add a picture too if possible :'(
 

Johan Schoeman

Expert
Licensed User
Longtime User
Yeah I know that text appear below scanner window, but I want to add additional text on specified position,add a picture too if possible :'(

It seems to me that this is unfortunatley the only way that the Zxing library allows you to pass text to be displayed when the camera is active.
 

Reids

Member
Licensed User
Longtime User
Okay 1 more question, I searching all forum but not yet find good answer, it is possible to add flash light while scaning? (without initial camera ex) because camera will lock while initial other camera library

Thank You verymuch!
 

JTmartins

Active Member
Licensed User
Longtime User
I also would like to enable flash in torch mode while scanning, but so far I had no sucess (my java knowledge is very limited). There are several people in the forum asking for this, but no one has never addressed this point as far as I can see.

If someone with a higher knowledge could spare a little time so that we can have this function, would be a great adition.

Thanks.
 

Johan Schoeman

Expert
Licensed User
Longtime User
I also would like to enable flash in torch mode while scanning, but so far I had no sucess (my java knowledge is very limited). There are several people in the forum asking for this, but no one has never addressed this point as far as I can see.

If someone with a higher knowledge could spare a little time so that we can have this function, would be a great adition.

Thanks.

Download QRCPV4 from link below. Use the volume up/down button to turn on/off the torch while scanning a code.

https://www.b4x.com/android/forum/threads/qr-code-library.41408/#post-259448
 
Last edited:

hibrid0

Active Member
Licensed User
Longtime User
Hello, I try this I see work so good.
But how can I rotate the scanner to vertical?
 

Johan Schoeman

Expert
Licensed User
Longtime User

Johan Schoeman

Expert
Licensed User
Longtime User
Hi and thanks for your quick answer, yes I try it before I ask, but the version on post #64 not work fine the auto focus.
It probable depends on your device. It works perfectly on my Samsung S4 mini. As for the project in post #1 of this thread - it was "designed" around the scanner being in landscape by @vpires and @icefairy333. You can change the orientation in the B4A manifest file but the view (while the scanner is active) will not be correct. So, best to use this one in landscape mode.
 
Top