Zxing NotFoundException with Barcode 128C - java

I try to use Zxing to decode 128C (Code set C) barcodes. I have success when I read other types like QR_CODE, UPC_A.
These are barcodes that I trying to read:
Is possible read 128C barcodes (Not CODE 128 pure) with Zxing ?

Short aswer, yes, it should is possible. As 128C just is a subset of 128. Can scan the codes, might take a few seconds. And have XZing working in a app.
You have found out that 128 is supported, now you got to make an translation. 128C takes the same input as 128, just turns out numbers. So you could make a translation from the data you get back, and turn it into 128C. Check the second link for how that translates.
https://github.com/zxing/zxing/blob/master/README.md
https://en.wikipedia.org/wiki/Code_128
Get the needed classes from the XZing github, put them in a package in the java part of your project. I used only 2:
IntentIntegrator
IntentResult
Here is how it is initiated in my code:
/**
* Method called to intiate the scan (it's linked to a button)
*/
public void doScan(View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
// when you click the Scan Bar Code button
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) { // we have a result
String scanContent = scanningResult.getContents(); // set the content of the scan.
String scanFormat = scanningResult.getFormatName(); // set the type of scan.
// You will want to put this data somewhere else, globals, etc.
} else {
toast("No scan data received!"); // call to make a toast
}
}

Related

Get Google Spreadsheet ID from Google Storage Access Framework Virtual File URI

I pick Google Spreadsheet with Intent.ACTION_OPEN_DOCUMENT and can get it's Virtual File URI.
But to use Google Sheets API, I need Spreadsheet ID, and it seems Virtual File URI doesn't contain it.
How can I get Google Spreadsheet ID from data, received by Intent.ACTION_OPEN_DOCUMENT?
My simplified code snippet:
public class MainActivity extends AppCompatActivity {
...
public void onMenuFile(MenuItem item) {
// called when 'File' menu item is selected
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
startActivityForResult(intent, EDIT_REQUEST_CODE);
}
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode != EDIT_REQUEST_CODE || resultCode != Activity.RESULT_OK || data == null) {
return;
}
uri = data.getData();
if (isVirtualFile(uri)) {
// here i would like to get spreadsheetID somehow, and use it:
ValueRange response = service.spreadsheets().values()
.get(spreadsheetID, sheetTitle)
.execute();
List<List<Object>> values = response.getValues();
...and so on...
}
...
}
...
private boolean isVirtualFile(URI uri)) {
here code from
https://developer.android.com/training/data-storage/shared/documents-files#java
}
}
Further researches show that Intent.ACTION_VIEW successfully find required spreadsheet by virtual file URI:
if (isVirtualFile(uri)) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} }
and actual activity class is com.google.android.apps.docs.app.OpenSafUrlActivity.
It would be nice to look into the code, but I could not find sources...
UPDATE
It seems that i've found a workaround here:
Find a Google Drive Sheet By Name Using API 4 In Android
SpreadsheetID needs to be hard coded in. See this google code snippet:
https://github.com/googleworkspace/java-samples/blob/master/sheets/snippets/src/main/java/SpreadsheetSnippets.java#L18-L30
You can only call getData() after the spread sheet object has been created.
To find out what the spreadhsheet id go to the desired sheet on google drive and look in the URL:
https://docs.google.com/spreadsheets/d/**spreadsheetId**/edit#gid=0
More info here: https://developers.google.com/sheets/api/guides/concepts

When i start the intent ACTION_OPEN_DOCUMENT_TREE it automatically opens empty recent folder?

What i'm trying to achieve is to delete a file on the sd card, i tried the file.delete method which didn't work because sd cards are read only now.
So i read a post about SAF (Storage Access Framework) to gain sd card write access by storing the treeUri we get in onActivityResult.
File deleting works fine now, but when i start the intent Intent.ACTION_OPEN_DOCUMENT_TREE sometimes it returns the recent folder which is empty and the way to show the files on the sdcard is to click on the overflow icon and then select show SDCARD or Internal Storage which may confuse some people when they run my app.
i tried adding these to my intent: intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
intent.putExtra("android.content.extra.FANCY", true);
intent.putExtra("android.content.extra.SHOW_FILESIZE", true);
which works on some devices, but it's a private API and on some it doesn't work.
So is there a way to like automatically open a specific directory or show a hint dialog with steps explaining which directory they should chose?
private void getSDCardAccess(){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath());
startActivityForResult(intent, REQUEST_EXTERNAL_ACCESS);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_EXTERNAL_ACCESS && resultCode == RESULT_OK) {
Uri treeUri = null;
if (data != null){
treeUri = data.getData();
}
if (treeUri != null && getActivity() != null) {
getActivity().getContentResolver().takePersistableUriPermission(treeUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
StorageUtil storageUtil = new StorageUtil(getActivity());
//Takes the access so that we can use it again after the app reopens
storageUtil.storeTreeUri(treeUri.toString());
Log.d(TAG, "treeUri: " + treeUri.toString());
}else{
Log.d(TAG,"uri is empty!");
}
}
}
is there a way to like automatically open a specific directory
If you have a Uri to it that you got from ACTION_OPEN_DOCUMENT_TREE previously, you should be able to supply that via DocumentsContract.EXTRA_INITIAL_URI. Per the ACTION_OPEN_DOCUMENT_TREE documentation:
Callers can set a document URI through DocumentsContract.EXTRA_INITIAL_URI to indicate the initial location of documents navigator. System will do its best to launch the navigator in the specified document if it's a folder, or the folder that contains the specified document if not.
or show a hint dialog with steps explaining which directory they should chose?
You would need to do that yourself, prior to calling startActivityForResult() for the ACTION_OPEN_DOCUMENT_TREE request.

Is there anyway to send the scanned QR code results directly to the server?

I have developed an android app for scanning QR Codes and send it over the server.
In this app i am getting multiple QR code scanned results in a list-view using an array list and sending them to the server on a button click event.
The requirement is to send all the scanned QR-code results directly to the
server without using any of the events e.g button click or something like that.
Here i am using zxing QR scanner using intent. Below is the code for getting the QR-code scan result.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SCANNER_REQUEST_CODE) {
// Handle scan intent
if (resultCode == Activity.RESULT_OK) {
// Handle successful scan
String contents = intent.getStringExtra("SCAN_RESULT");
String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
int intentOrientation = intent.getIntExtra(
"SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
Integer orientation = (intentOrientation == Integer.MIN_VALUE) ? null
: intentOrientation;
String errorCorrectionLevel = intent
.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
QRContent = contents;
QR_Receivd.setText(QRContent);
listItems.add(QRContent); //adding scan result to an array list(listItems)
} else if (resultCode == Activity.RESULT_CANCELED) {
// Handle cancel
}
//On-click method to initiate scan.
#Override
public void onClick(View v) {
if (v.getId() == R.id.btScan) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "SCAN_MODE");
startActivityForResult(intent, SCANNER_REQUEST_CODE);
}
}
So instead of using on-click event i want to send the scanned result directly to a server immidiately after getting the result from the scanner.
Thanks!!!
Part 1
first you should pick how you want to send it. you can send it using the official tools, or using 3rd party libraries. i'll assume the later.
on my approach you will need, on the server, a PHP file in charge of recieving (by GET or by POST) the scanned QR as parameter.
example call :
myserver.com/API/addqr?qrcode=XXXXXXXXXX
this PHP file must then save that to your database, i suppose MYSQL.
PART 2
now that you have your server set, you should take a look at the different libs or the official documentation.
USING VOLLEY
Using Loopj's Android Async HTTP petitions (i reccomend this)
i will assume you pick method 2.
all you have to do is send the petition :
if (resultCode == Activity.RESULT_OK) {
//blah blah blah all the code that decodes and adds it to the listview
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("qrcode", "XXXXXXXXXXXXX");
client.post("myserver.com/API/addqr", params, new JsonHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//do something with the response, if there is one.
}
});
}

Trying to call the JavaScript function from Java Source code

I am developing an Android App to read the barcode by using the Zxing Barcode reader Plugin.
In the plugin there is an object named window.plugins.barcodeScanner with which we encode/decode the barcode.
I don't wanna use HTML to invoke things instead want the below Javascript function to be called from Java [on click of the image- the below function would be invoked].
function scanCode(){
window.plugins.barcodeScanner.scan(
function(result){
alert("Scanned Code: " + result.text
+ ". Format: " + result.format
+ ". Cancelled: " + result.cancelled);
},
function(error){
alert("Scan failed: " + error);
}
);
}
Kindly let me know how to achieve this.
Assumptions:
You've already setup the LibararyProject from https://github.com/wildabeast/BarcodeScanner/tree/master/src/android.
Your Activity is not extending CordovaActivity but is extending Activity.
Your main goal is really just to use the scanner. You were just trying to find an easy/quick way to do so and thought the PG plugin may do the trick.
All you have to do is pull out the scan and onActivityResult methods and some of the helper strings from https://github.com/wildabeast/BarcodeScanner/blob/master/src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java and put them in your activity. You'll need to replace the references to cordova with your own activity.
End result may look something like this:
public static final int REQUEST_CODE = 0x0ba7c0de;
private static final String SCAN_INTENT = "com.google.zxing.client.android.SCAN";
public void scan() {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
this.startActivityForResult(intentScan, REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String barcode = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
//Do whatever you need with the barcode here
} else if (resultCode == Activity.RESULT_CANCELED) {
// handle a canceled scan
} else {
// throw an error or something
}
}
}
If that works for you, then you don't even need cordova as a dependancy.
You can invoke Javascript code from native side on Android using the sendJavascript function defined in CordovaWebView.
In your case you would do something like this. Considering that the you want to call the following function:
function scanSuccessCallback(result) {
//do something
}
on the native side in your plugin:
this.webView.sendJavascript("scanSuccessCallback('the result');");

Dropbox Sync API errors

I have downloaded and installed the Dropbox Sync API for Android from here: https://www.dropbox.com/developers/sync
I couldn't understand much of their tutorial. It lacked a lot of crucial variables, so that I had to find them out myself. I now have a very messy code and a lot of things aren't there.
private DbxAccountManager mDbxAcctMgr;
static final int REQUEST_LINK_TO_DBX = 0; // This value is up to you // thanks for telling what it actually does, Dropbox!
DbxPath path = new DbxPath("/test.pdf");
public void onClickLinkToDropbox(View view) {
mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_LINK_TO_DBX) {
if (resultCode == Activity.RESULT_OK) {
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount()); // try /catch error
DbxFile testFile = dbxFs.open(path); //try/catch error
String contents = testFile.readString();
Log.d("Dropbox Test", "File contents: " + contents); // try / catch error
} else {
// ... Link failed or was cancelled by the user.
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
This gives me errors on the 3 lines, saying that I need to add try/catch.
But when I add try/catch, Eclipse tells me to null the variables, which would mean all the functions would only read nulls, thus not reading anything..
Could anyone help me any further? Dropbox only confused me more by giving wrong instructions etc. I am sure that even if I didn't have 3 try/catch errors it would still not work.
Anyone here who has experience with the Dropbox Sync API and could share an example program reading a txt file in a User's Dropbox folder?
Bart
For more info on REQUEST_LINK_TO_DBX, check out the Android documentation for onActivityResult: https://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent). It's there to help you differentiate between different activities.
For a working example, I'd suggest looking at the HelloDropbox example that comes with the SDK. Among other things, it reads a string from a file in Dropbox.
Here's the relevant code from HelloDropboxActivity.java:
try {
// ...
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
// ...
String resultData;
DbxFile testFile = dbxFs.open(testPath);
try {
resultData = testFile.readString();
} finally {
testFile.close();
}
} catch (IOException e) {
// ...
}

Categories

Resources