Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Formats Parameter #110

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Arguments:
camera: "front" || "back" // defaults to "back"
flash: "on" || "off" || "auto" // defaults to "auto". See Quirks
drawSight: true || false //defaults to true, create a red sight/line in the center of the scanner view.
formats: ["QRCODE","CODE128"] //limit scan to formats listed, leave blank if no restriction is desired.
}
```

Expand Down
46 changes: 39 additions & 7 deletions android/ZBarScannerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.RuntimeException;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

import android.Manifest;
import android.app.Activity;
Expand Down Expand Up @@ -141,15 +142,46 @@ private void setUpCamera() {
whichCamera = params.optString("camera");
flashMode = params.optString("flash");

//new parameter formats - list of formats to scan
JSONArray formats = params.optJSONArray("formats");


// Initiate instance variables
autoFocusHandler = new Handler();
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);


// Set the config for barcode formats
for(ZBarcodeFormat format : getFormats()) {
scanner.setConfig(format.getId(), Config.ENABLE, 1);
if (formats.length() > 0){

//disable all
for(ZBarcodeFormat format : getFormats()) {
scanner.setConfig(format.getId(), Config.ENABLE, 0);
}

//enable only those that are listed
int len = formats.length();
for (int i=0;i<len;i++){
String name;
try {
name = formats.get(i).toString();
}catch(JSONException e){
throw new RuntimeException(e);
}

if (name != null){
ZBarcodeFormat format = ZBarcodeFormat.getFormatByName(name);
scanner.setConfig(format.getId(), Config.ENABLE, 1);
}
}

} else {
// no format specified, read all
for(ZBarcodeFormat format : getFormats()) {
scanner.setConfig(format.getId(), Config.ENABLE, 1);
}
}

// Set content view
Expand Down Expand Up @@ -334,7 +366,7 @@ public void onConfigurationChanged(Configuration newConfig)
}

public void toggleFlash(View view) {
camera.startPreview();
camera.startPreview();
android.hardware.Camera.Parameters camParams = camera.getParameters();
//If the flash is set to off
try {
Expand All @@ -346,7 +378,7 @@ public void toggleFlash(View view) {

}

try {
try {
// camera.setParameters(camParams);
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(previewCb);
Expand All @@ -366,8 +398,8 @@ public void toggleFlash(View view) {
} catch(RuntimeException e) {
Log.d("csZBar", (new StringBuilder("Unsupported camera parameter reported for flash mode: ")).append(flashMode).toString());
} catch (IOException e) {
Log.d("csZBar", (new StringBuilder("Wrong holder data")).append(flashMode).toString());
}
Log.d("csZBar", (new StringBuilder("Wrong holder data")).append(flashMode).toString());
}
}
// Continuously auto-focus -----------------------------------------
// For API Level < 14
Expand Down Expand Up @@ -532,7 +564,7 @@ private void tryStartPreview () {
camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(camParams);
} catch (Exception e) {
// TODO: don't swallow
// TODO: don't swallow
}

camera.setPreviewDisplay(holder);
Expand Down
12 changes: 12 additions & 0 deletions android/ZBarcodeFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;
import java.util.ArrayList;
import java.util.Objects;

public class ZBarcodeFormat {
private int mId;
Expand Down Expand Up @@ -69,4 +70,15 @@ public static ZBarcodeFormat getFormatById(int id) {
}
return ZBarcodeFormat.NONE;
}

//new method to get the format by name
public static ZBarcodeFormat getFormatByName(String name){
for (ZBarcodeFormat format : ALL_FORMATS){
if(Objects.equals(format.getName(),name)) {
return format;
}
}
return ZBarcodeFormat.NONE;
}

}
1 change: 1 addition & 0 deletions www/zbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ZBar.prototype = {
if(params.text_instructions === undefined) params.text_instructions = "Please point your camera at the QR code.";
if(params.camera != "front") params.camera = "back";
if(params.flash != "on" && params.flash != "off") params.flash = "auto";
if(!params.formats) params.formats = [];

exec(success, failure, 'CsZBar', 'scan', [params]);
},
Expand Down