Giter Site home page Giter Site logo

photobarcode's Introduction

PhotoBarcode

Easy to use library to take picture or scanning barcodes in the Android application.

Dependency

Add this in your root build.gradle file (not your module build.gradle file):

allprojects {
	repositories {
        maven { url "https://jitpack.io" }
    }
}

Then, add the library to your module build.gradle

dependencies {
    implementation 'com.github.ivan200:PhotoBarcode:1.1.1'
}

Usage

To taking picture

fabPicture.setOnClickListener(view -> {
    final PhotoBarcodeScanner photoBarcodeScanner = new PhotoBarcodeScannerBuilder()
            .withActivity(this)
            .withTakingPictureMode()
            .withPictureListener(file -> {
                imageView.setImageURI(Uri.fromFile(file));
            })
            .build();
    photoBarcodeScanner.start();
});

To scan a barcode

fabBarcode.setOnClickListener(view -> {
    final PhotoBarcodeScanner photoBarcodeScanner = new PhotoBarcodeScannerBuilder()
            .withActivity(this)
            .withResultListener((Barcode barcode) -> {
                textView.setText(barcode.rawValue);
            })
            .build();
    photoBarcodeScanner.start();
});

That's it!

Or you can customize the builder with a lot more options:

Additional parameters with default values:

Flags for both modes:

.withCameraFullScreenMode(false)       //Mode of camera preview: FullScreen - 16/9 with horizontal crop, or otherwise 4/3 with screen fit
.withRequestedFps(25.0f)               //Fps in preview of picture.
.withSoundEnabled(true)                //Enables or disables a sound whenever picture taken or a barcode is scanned
.withAutoFocus(true)                   //Enables or disables auto focusing on the camera
.withFocusOnTap(true)                  //Allow focus picture when user tap on screen
.withFlashMode(FlashMode)              //Setup default flash mode before open camera
.withFlashListener(Consumer<FlashMode>)//Sets handler when flash changed (for ability to save the last used flash mode in settings)
.withCameraFacingBack(true)            //Use the camera facing back or front
.withCameraLockRotate(true)            //Lock rotate phone and orientation in camera activity (to avoid recreating view)
.withErrorListener(ex->{showAlert()})  //Possibility to customize fatal exceptions occured 
.withMinorErrorHandler(printStackTrace)//Possibility to customize handler of non fatal exceptions
.withCancelListener(Runnable)          //Called when back pressed in camera activity or dialog for request permission is cancelled  

Flags for picture mode:

.withTakingPictureMode()               //Activate takingPicture mode instead of taking barcode (barcode mode is default)
.withPreviewImage(true)                //Allow preview image and redo it before it returned
.withPictureListener(Consumer<File>)   //Set listener to take picture, file will saved in context.getFilesDir()/photos
.withFacingListener(Consumer<Boolean>) //Sets listener when camera facing changed (for ability to save the last used camera facing in settings)
.withFlipFaceFrontResultImage(false)   //Enables or disables flip result image of facing front camera
.withThumbnails(false)                 //In addition to the photo the thumbnail will be saved too (in context.getFilesDir()/thumbnails)
.withCameraTryFixOrientation(true)     //Automatically try to rotate final image by phone sensors
.withImageLargerSide(1200)             //Once the picture is taken, if its too big, it automatically resizes by the maximum side
.withSavePhotoToGallery(String)        //Once the picture is taken, it automatically saved into phone gallery as well (DCIM directory)
                                       //You need to have WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions in your manifest file to use it

Flags for barcode mode:

.withCenterTracker(false)              //Enables the default center tracker (white square in screen)
.withResultListener(Consumer<Barcode>) //Called immediately after a barcode was scanned
.withText("Scanning...")               //Shows a text message at the top of the barcode scanner
.withTrackerColor(int)                 //Sets the tracker color used by the barcode scanner (default is "#F44336")
.withCenterTracker(int, int)           //Customize center tracker with a custom drawable resource
.withOnly2DScanning()                  //Setup scan only 2D type of barcodes
.withOnly3DScanning()                  //Setup scan only 3D type of barcodes
.withOnlyQRCodeScanning()              //Setup scan only QR-Code barcodes
.withBarcodeFormats(int)               //Bit mask that selects which formats this barcode detector should recognize.

Notice

If you want the library to fully handle requests permissions, you need to pass onActivityResult and onRequestPermissionsResult to PhotoBarcodeScanner, for example like this:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    photoBarcodeScanner.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    photoBarcodeScanner.onActivityResult(requestCode, resultCode, data);
}

Or you can manage permissions by yourself before calling photoBarcodeScanner.start();

Screenshots

Developed By

photobarcode's People

Contributors

alirezazarghi avatar ivan200 avatar vicktor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.