1
0

Merge pull request #9983 from f321x/change_android_qr_lib

android: replace qr code scanning library
This commit is contained in:
ghost43
2025-07-04 13:02:46 +00:00
committed by GitHub
6 changed files with 195 additions and 17 deletions

1
.gitignore vendored
View File

@@ -39,6 +39,7 @@ contrib/build-linux/appimage/.cache/
contrib/osx/.cache/
contrib/osx/build-venv/
contrib/android/android_debug.keystore
contrib/android/.cache/
contrib/secp256k1/
contrib/zbar/
contrib/libusb/

View File

@@ -156,13 +156,20 @@ android.accept_sdk_license = True
android.add_jars = .buildozer/android/platform/*/build/libs_collections/Electrum/jar/*.jar
android.add_aars =
contrib/android/.cache/aars/BarcodeScannerView.aar,
contrib/android/.cache/aars/CameraView.aar,
contrib/android/.cache/aars/zxing-cpp.aar
# (list) List of Java files to add to the android project (can be java or a
# directory containing the files)
android.add_src = electrum/gui/qml/java_classes/
# kotlin-stdlib is required for zxing-cpp (BarcodeScannerView)
android.gradle_dependencies =
com.android.support:support-compat:28.0.0,
me.dm7.barcodescanner:zxing:1.9.8
org.jetbrains.kotlin:kotlin-stdlib:1.8.22
android.add_activities = org.electrum.qr.SimpleScannerActivity

View File

@@ -86,16 +86,22 @@ if [[ "$2" == "all" ]] ; then
# FIXME failures are not propagated out: we should fail the script if any arch build fails
export APP_ANDROID_ARCHS=armeabi-v7a
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
export APP_ANDROID_ARCHS=arm64-v8a
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
export APP_ANDROID_ARCHS=x86_64
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
else
export APP_ANDROID_ARCHS=$2
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
fi

View File

@@ -0,0 +1,144 @@
#!/bin/bash
# script to clone and build https://github.com/markusfisch/BarcodeScannerView and its dependencies,
# https://github.com/markusfisch/CameraView/ and https://github.com/markusfisch/zxing-cpp
# which are being used as barcode scanner in the Android app.
# To bump the version of BarcodeScannerView, get the newest version tag from the github repo,
# then get the required dependencies from
# https://github.com/markusfisch/BarcodeScannerView/blob/**VERSION_TAG**/barcodescannerview/build.gradle
# then update the commit hashes below. Also update kotlin-stdlib in buildozer_qml.spec to the
# "kotlin-version" specified in the used zxing-cpp commit:
# https://github.com/markusfisch/zxing-cpp/blob/master/wrappers/aar/build.gradle
BARCODE_SCANNER_VIEW_COMMIT_HASH="a4928bf83c0aae8ecb80e665d93f10b70232455b" # 1.6.3
BARCODE_SCANNER_VIEW_REPO="https://github.com/markusfisch/BarcodeScannerView.git"
CAMERA_VIEW_COMMIT_HASH="745597d05bc6abfdb3637a09a8ecaf30fdce7b6e" # 1.10.0
CAMERA_VIEW_REPO="https://github.com/markusfisch/CameraView.git"
ZXING_CPP_COMMIT_HASH="0741a597409ff69a96a326f3a65fe6440d87ad99" # v2.2.0.5 using kotlin-stdlib 1.8.22
ZXING_CPP_REPO="https://github.com/markusfisch/zxing-cpp.git"
########################################################################################################
set -e
CONTRIB_ANDROID="$(dirname "$(readlink -e "$0")")"
CONTRIB="$CONTRIB_ANDROID"/..
CACHEDIR="$CONTRIB_ANDROID/.cache"
BUILDDIR="$CACHEDIR/builds"
. "$CONTRIB"/build_tools_util.sh
# target architecture passed as argument by`make_apk.sh`
TARGET_ARCH="$1"
# check if TARGET_ARCH is set and supported
if [[ "$TARGET_ARCH" != "armeabi-v7a" \
&& "$TARGET_ARCH" != "arm64-v8a" \
&& "$TARGET_ARCH" != "x86_64" ]]; then
fail "build_barcode_scanner.sh invalid target architecture argument: $TARGET_ARCH"
fi
info "Building BarcodeScannerView and deps for architecture: $TARGET_ARCH"
# check if directories exist, create them if not
if [ ! -d "$CACHEDIR/aars" ]; then
mkdir -p "$CACHEDIR/aars"
fi
if [ ! -d "$BUILDDIR" ]; then
mkdir -p "$BUILDDIR"
fi
####### zxing-cpp ########
# check if zxing-cpp aar is already in cachedir, else build it
ZXING_CPP_BUILD_ID="$TARGET_ARCH-$ZXING_CPP_COMMIT_HASH"
if [ -f "$CACHEDIR/aars/zxing-cpp-$ZXING_CPP_BUILD_ID.aar" ]; then
info "zxing-cpp for $ZXING_CPP_BUILD_ID already exists in cache, skipping build."
cp "$CACHEDIR/aars/zxing-cpp-$ZXING_CPP_BUILD_ID.aar" "$CACHEDIR/aars/zxing-cpp.aar"
else
info "Building zxing-cpp for $ZXING_CPP_BUILD_ID..."
ZXING_CPP_DIR="$BUILDDIR/zxing-cpp"
clone_or_update_repo "$ZXING_CPP_REPO" "$ZXING_CPP_COMMIT_HASH" "$ZXING_CPP_DIR"
cd "$ZXING_CPP_DIR/wrappers/aar"
chmod +x gradlew
# Set local.properties to use SDK of docker container
echo "sdk.dir=${ANDROID_SDK_HOME}" > local.properties
# gradlew will install a specific NDK version required by zxing-cpp
./gradlew :zxingcpp:assembleRelease -Pandroid.injected.build.abi="$TARGET_ARCH"
# Copy the built AAR to cache directory
ZXING_AAR_SOURCE="$ZXING_CPP_DIR/wrappers/aar/zxingcpp/build/outputs/aar/zxingcpp-release.aar"
ZXING_AAR_DEST_GENERIC="$CACHEDIR/aars/zxing-cpp.aar"
ZXING_AAR_DEST_SPECIFIC="$CACHEDIR/aars/zxing-cpp-$ZXING_CPP_BUILD_ID.aar"
if [ ! -f "$ZXING_AAR_SOURCE" ]; then
fail "zxing-cpp AAR not found at $ZXING_AAR_SOURCE, build failed?"
fi
cp "$ZXING_AAR_SOURCE" "$ZXING_AAR_DEST_GENERIC"
# keeping an arch specific copy allows to skip the build later if it already exists
cp "$ZXING_AAR_SOURCE" "$ZXING_AAR_DEST_SPECIFIC"
info "zxing-cpp AAR copied to $ZXING_AAR_DEST_GENERIC"
fi
########### CameraView ###########
CAMERA_VIEW_BUILD_ID="$CAMERA_VIEW_COMMIT_HASH"
if [ -f "$CACHEDIR/aars/CameraView-$CAMERA_VIEW_BUILD_ID.aar" ]; then
info "CameraView AAR already exists in cache, skipping build."
cp "$CACHEDIR/aars/CameraView-$CAMERA_VIEW_BUILD_ID.aar" "$CACHEDIR/aars/CameraView.aar"
else
info "Building CameraView..."
CAMERA_VIEW_DIR="$BUILDDIR/CameraView"
clone_or_update_repo "$CAMERA_VIEW_REPO" "$CAMERA_VIEW_COMMIT_HASH" "$CAMERA_VIEW_DIR"
cd "$CAMERA_VIEW_DIR"
chmod +x gradlew
echo "sdk.dir=${ANDROID_SDK_HOME}" > local.properties
./gradlew :cameraview:assembleRelease
CAMERA_AAR_SOURCE="$CAMERA_VIEW_DIR/cameraview/build/outputs/aar/cameraview-release.aar"
CAMERA_AAR_DEST_GENERIC="$CACHEDIR/aars/CameraView.aar"
CAMERA_AAR_DEST_SPECIFIC="$CACHEDIR/aars/CameraView-$CAMERA_VIEW_BUILD_ID.aar"
if [ ! -f "$CAMERA_AAR_SOURCE" ]; then
fail "CameraView AAR not found at $CAMERA_AAR_SOURCE"
fi
cp "$CAMERA_AAR_SOURCE" "$CAMERA_AAR_DEST_GENERIC"
cp "$CAMERA_AAR_SOURCE" "$CAMERA_AAR_DEST_SPECIFIC"
info "CameraView AAR copied to $CAMERA_AAR_DEST"
fi
########### BarcodeScannerView ###########
BARCODE_SCANNER_VIEW_BUILD_ID="$BARCODE_SCANNER_VIEW_COMMIT_HASH"
if [ -f "$CACHEDIR/aars/BarcodeScannerView-$BARCODE_SCANNER_VIEW_BUILD_ID.aar" ]; then
info "BarcodeScannerView AAR already exists in cache, skipping build."
cp "$CACHEDIR/aars/BarcodeScannerView-$BARCODE_SCANNER_VIEW_BUILD_ID.aar" "$CACHEDIR/aars/BarcodeScannerView.aar"
else
info "Building BarcodeScannerView..."
BARCODE_SCANNER_VIEW_DIR="$BUILDDIR/BarcodeScannerView"
clone_or_update_repo "$BARCODE_SCANNER_VIEW_REPO" "$BARCODE_SCANNER_VIEW_COMMIT_HASH" "$BARCODE_SCANNER_VIEW_DIR"
cd "$BARCODE_SCANNER_VIEW_DIR"
chmod +x gradlew
echo "sdk.dir=${ANDROID_SDK_HOME}" > local.properties
./gradlew :barcodescannerview:assembleRelease
BARCODE_AAR_SOURCE="$BARCODE_SCANNER_VIEW_DIR/barcodescannerview/build/outputs/aar/barcodescannerview-release.aar"
BARCODE_AAR_DEST_GENERIC="$CACHEDIR/aars/BarcodeScannerView.aar"
BARCODE_AAR_DEST_SPECIFIC="$CACHEDIR/aars/BarcodeScannerView-$BARCODE_SCANNER_VIEW_BUILD_ID.aar"
if [ ! -f "$BARCODE_AAR_SOURCE" ]; then
fail "BarcodeScannerView AAR not found at $BARCODE_AAR_SOURCE"
fi
cp "$BARCODE_AAR_SOURCE" "$BARCODE_AAR_DEST_GENERIC"
cp "$BARCODE_AAR_SOURCE" "$BARCODE_AAR_DEST_SPECIFIC"
info "BarcodeScannerView AAR copied to $BARCODE_AAR_DEST"
fi
info "All barcode scanner libraries built successfully for $TARGET_ARCH"

View File

@@ -50,6 +50,28 @@ function download_if_not_exist() {
fi
}
# Function to clone or update a git repository to a specific commit
clone_or_update_repo() {
local repo_url=$1
local commit_hash=$2
local repo_dir=$3
if [ -z "$repo_url" ] || [ -z "$commit_hash" ] || [ -z "$repo_dir" ]; then
fail "clone_or_update_repo: invalid arguments: repo_url='$repo_url', commit_hash='$commit_hash', repo_dir='$repo_dir'"
fi
if [ -d "$repo_dir" ]; then
info "Repository $repo_url exists in $repo_dir, updating..."
git -C "$repo_dir" clean -ffxd >/dev/null 2>&1 || fail "Failed to clean repository $repo_dir"
git -C "$repo_dir" fetch --all >/dev/null 2>&1 || fail "Failed to fetch from repository"
git -C "$repo_dir" reset --hard "$commit_hash^{commit}" >/dev/null 2>&1 || fail "Failed to reset to commit $commit_hash"
else
info "Cloning repository: $repo_url to $repo_dir"
git clone "$repo_url" "$repo_dir" >/dev/null 2>&1 || fail "Failed to clone repository $repo_url"
git -C "$repo_dir" checkout "$commit_hash^{commit}" >/dev/null 2>&1 || fail "Failed to checkout commit $commit_hash"
fi
}
# https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/templates/header.sh
function retry() {
local result=0

View File

@@ -20,17 +20,15 @@ import androidx.core.app.ActivityCompat;
import java.util.Arrays;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import de.markusfisch.android.barcodescannerview.widget.BarcodeScannerView;
import com.google.zxing.Result;
import com.google.zxing.BarcodeFormat;
import org.electrum.electrum.res.R; // package set in build.gradle
public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
public class SimpleScannerActivity extends Activity {
private static final int MY_PERMISSIONS_CAMERA = 1002;
private ZXingScannerView mScannerView = null;
private BarcodeScannerView mScannerView = null;
final String TAG = "org.electrum.SimpleScannerActivity";
private boolean mAlreadyRequestedPermissions = false;
@@ -85,23 +83,23 @@ public class SimpleScannerActivity extends Activity implements ZXingScannerView.
public void onPause() {
super.onPause();
if (null != mScannerView) {
mScannerView.stopCamera(); // Stop camera on pause
mScannerView.close(); // Stop camera on pause
}
}
private void startCamera() {
mScannerView = new ZXingScannerView(this);
mScannerView.setFormats(Arrays.asList(BarcodeFormat.QR_CODE));
mScannerView = new BarcodeScannerView(this);
mScannerView.setCropRatio(0.75f); // Set crop ratio to 75% (this defines the square area shown in the scanner view)
// by default only Format.QR_CODE is set
ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
contentFrame.addView(mScannerView);
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void handleResult(Result rawResult) {
//resultIntent.putExtra("format", rawResult.getBarcodeFormat().toString());
this.setResultAndClose(rawResult.getText());
mScannerView.setOnBarcodeListener(result -> {
// Handle the scan result
this.setResultAndClose(result.getText());
// Return false to stop scanning after first result
return false;
});
mScannerView.openAsync(); // Start camera on resume
}
private void setResultAndClose(String resultText) {