2

How can I change the viewfinder size, colors and other options when I'm using dependencies compile 'me.dm7.barcodescanner:zxing:1.9' without an access to the zxing files? I could not find a solution for this problem.

mikro098
  • 2,173
  • 2
  • 32
  • 48

3 Answers3

2

to change the color of the ViewFinder. add these to your project's colors.xml

<color name="viewfinder_mask">#3F51B5</color>
<color name="viewfinder_laser">#00FFFFFF</color>
<color name="viewfinder_border">#ffFF4081</color>

to make other changes like removing laser from ZXingScannerView's viewfinder create a class and extend ViewFinderView. Override methods to customize the UI like so.

public class CustomScannerView extends ZXingScannerView {

public CustomScannerView(Context context) {
    super(context);
}

@Override
protected IViewFinder createViewFinderView(Context context) {
    return new CustomViewFinderView(context);
}

//make changes in CustomViewFinderView to customise the Viewfinder
//Check ViewFinderView class for more modifications
//to change viewFinder's colours override appropriate values in Colors.xml
class CustomViewFinderView extends ViewFinderView {


    public CustomViewFinderView(Context context) {
        super(context);
        setSquareViewFinder(true);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        // DEFAULT SQUARE DIMENSION RATIO in ViewFinderView is 0.625
        // get appropriate Dimension ratio otherwise
        float width = displayMetrics.widthPixels * 0.625f;
        setBorderLineLength((int)width);
    }

    @Override
    public void drawLaser(Canvas canvas) {
        //do nothing for no laser, even remove super call
    }

 }
}

to set the viewfinder's rectangle use

super.getFramingRect().set(left, top, right, bottom);
Akshay More
  • 416
  • 4
  • 9
2

If you want to remove the laser only, this can simply be done by disabling the alpha channel animation:

barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);

ViewfinderView viewFinder = barcodeView.getViewFinder();
Field scannerAlphaField = null;
try {
    scannerAlphaField = viewFinder.getClass().getDeclaredField("SCANNER_ALPHA");
    scannerAlphaField.setAccessible(true);
    scannerAlphaField.set(viewFinder, new int[1]);
} catch (NoSuchFieldException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Robert
  • 1,357
  • 15
  • 26
1

You can do it via XML

<com.journeyapps.barcodescanner.BarcodeView
      android:id="@+id/zxing_barcode_surface"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:zxing_framing_rect_height="220dp"
      app:zxing_framing_rect_width="250dp" />

<com.journeyapps.barcodescanner.ViewfinderView
    android:id="@+id/zxing_viewfinder_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:zxing_possible_result_points="@color/zxing_custom_possible_result_points"
    app:zxing_result_view="@color/zxing_custom_result_view"
    app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser"
    app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" />

Also check this post (possible duplicate this question), and another one, if you want to change behavior programmatically.

To access the library features, you need to add the following to your build.gradle file:

repositories {
    jcenter()
}

dependencies {
    compile 'me.dm7.barcodescanner:zxing:1.9.3'
}

And add camera permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA" />
Tim
  • 176
  • 9
  • but how can I install it to get access to its classes? I'm new to Android development. – mikro098 Jul 06 '17 at 06:11
  • @codddeer123 just add dependency `compile 'me.dm7.barcodescanner:zxing:1.9.3'` to application module with Gradle. Also you can use the installation guide from [Github project](https://github.com/dm77/barcodescanner#installation). After that start sync project, then you can use the classes and views from library. – Tim Jul 07 '17 at 07:27
  • inside External Libraries -> zxing-1.9? There is only BuildConfig and ZXingScannerView – mikro098 Jul 07 '17 at 08:02
  • @codddeer123 please, check my updated answer. As I said, you have 2 build.gradle files. The 1st one for whole project, the 2nd one for certain module. – Tim Jul 07 '17 at 08:51
  • Error:(4) No resource identifier found for attribute 'zxing_framing_rect_height' in package 'android' I am getting this error when I am adding zxing_framing_rect_height – Shailesh Nov 25 '17 at 07:27