When utilizing an ionic project, there's no need to write a barcode scanner in native Java code as there are plenty of plugins available for Cordova. Utilizing the ionic / cordova
barcode scanner plugin would simplify your task.
However, for your specific situation:
Upon scanning the image, you will receive the image file path. Assign this path to a instance variable within the Java activity class. In your view, set up a setInterval timer and invoke a Java interface method that retrieves the file path. Once the file path is returned by the getter interface, clear both the Java instance variable and the interval.
Here's an example code snippet:
@JavascriptInterface
public void scanBarCode() {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
}
/** Read barcode from ZXing library */
@JavascriptInterface
public String getBarCode() {
return (barcode!=null ? barcode : "");
}
In your onActivityResult
function, you will handle the image URL return.
public void onActivityResult(int requestCode, int resultCode, Intent intent)
Next, in your JavaScript:
var barcodeTimer = setInterval(function () {
var barcode = Android.getBarCode();
if (barcode != "") {
// assign it to your $scope
$scope.$apply();
clearInterval(barcodeTimer);
}
}, 2000);