Giter Site home page Giter Site logo

php-zxing's Introduction

PHPZxing - Wrapper for Zxing Java Library

PHPZxing is a small php wrapper that uses the Zxing library to Create and read Barcodes. Under the hood it still uses the Zxing library to encode and decode data.

Install using composer

{  
    "require": {
        "dsiddharth2/php-zxing": "1.0.3"
    }  
}

Note

  • Only Decoder is programmed right now. Needs programming of Encoder.
  • The Default location of java that is configured is /usr/bin/java

Changes in version 1.0.3

  • Functionality added for possible_formats to work

Changes in version 1.0.2

  • Updated the new jars and tested on windows system

Changes in version 1.0.1

  • Added a isFound function that will tell if the bar code is found.
  • If the image has one bar code detected, then it returns the object instead of array of a single object.

Basic Usage

use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$data           = $decoder->decode('../images/Code128Barcode.jpg');
if($data->isFound()) {
    $data->getImageValue();
    $data->getFormat();
    $data->getType();        
}

The Decoded data is an Array of Objects of PHPZxing\ZxingImage if the bar code is found. If not found then it is an array of Objects PHPZxing\ZxingBarNotFound.

Checking for existence of Barcode

The Existance of bar code can be found using the functoin isFound()

use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$data           = $decoder->decode('../images/Code128Barcode.jpg');
if($data->isFound()) {
    $data->getImageValue();
    $data->getFormat();
    $data->getType();        
}

You can also check using the instanceof object,

use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$data           = $decoder->decode('../images/Code128Barcode.jpg');
if($data instanceof PHPZxing\ZxingImage) {
    $data->getImageValue();
    $data->getFormat();
    $data->getType();
}

The Public methods that we can use in PHPZxing\ZxingImage are,

Method Name Function
getImageValue Get the value decoded in the image passed
getFormat Get the format of the image that is encoded, example : CODE_39
getType Get the type of the image decoded, example : URL, TEXT etc
getImagePath Get Path of the image

The Public methods that we can use in PHPZxing\ZxingImage are,

Method Name Function
getImageErrorCode Get the error code for the image not found
getErrorMessage Error Message
getImagePath Get Path of the image

Setting the configurations

use PHPZxing\PHPZxingDecoder;

$config = array(
    'try_harder'            => true,
);
$decoder        = new PHPZxingDecoder($config);
$decodedArray   = $decoder->decode('../images');
if(is_array($decodedArray)){
    foreach ($decodedArray as $data) {
        if($data->isFound()) {
            print_r($data);
        }
    }
}

You can also use it with configurations. The Decoder has 4 configurations,

Config Name Function
try_harder If the image has bar/Qr code at unknown locations, then use this non mobile mode.
multiple_bar_codes If the image has multiple bar codes you want to read.
crop Crop the image and it will read only the cropped portion
possible_formats List of formats to decode, where format is any value in BarcodeFormat

More Examples

You can pass array of images too,

use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$imageArrays = array(
    '../images/Code128Barcode.jpg',
    '../images/Code39Barcode.jpg'
);
$decodedArray  = $decoder->decode($imageArrays);
foreach ($decodedArray as $data) {
    if($data instanceof PHPZxing\ZxingImage) {
        print_r($data);
    } else {
        echo "Bar Code cannot be read";
    }
}

Reading multiple bar codes,

use PHPZxing\PHPZxingDecoder;

$config = array(
    'try_harder' => true,
    'multiple_bar_codes' => true
);
$decoder        = new PHPZxingDecoder($config);
$decodedData    = $decoder->decode('../images/multiple_bar_codes.jpg');
print_r($decodedData);

Set Java Path

If your java PATH is not set properly, the decoder will not work. You need to set path of java variable.

use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$decoder->setJavaPath('/your/path/to/java');
$decodedData    = $decoder->decode('../images/Code128Barcode.jpg');
print_r($decodedData);

Where is my java located ?

If you do not know the path to java, then you can use the following on *nix enviromnents

$ which java

On Windows environment,

> where java

For more info, on Windows read the follwoing stackoverflow Link

Acknowledgments

Contibution

Please Contribute or suggest changes.

php-zxing's People

Contributors

alb3ric avatar dsiddharth2 avatar matrad avatar sid-mildlyclassic avatar sidatapra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-zxing's Issues

Error Call to a member function isFound() on null

This is my code

file('archivo'); $foto->storeAs('public', 'image.png'); $ruta ='public/image.png'; //Decodificar una imagen $config = array( 'try_harder' => true, 'multiple_bar_codes' => true ); $decoder = new PHPZxingDecoder($config); $decoder->setJavaPath('C:\Program Files\Java\jdk1.8.0_291'); //$decoder->setJavaPath('C:\Program Files\Java\jdk1.8.0_291\bin'); //C:\Program Files\Common Files\Oracle\Java\javapath\ //C:\Program Files\Common Files\Oracle\Java\javapath\java.exe $data = $decoder->decode($ruta); $ruta=''; $format=''; $type=''; /*When calling the isFound () function it is when it presents the error, do you have any idea why it will be? I'm new to Laravel, sorry for the interruption*/ if($data->isFound()) { $ruta = $data->getImageValue(); $format=$data->getFormat(); $type=$data->getType(); } return view('Testimage', ['ruta' => $ruta]); } }

Java path

Is the java PATH set correctly ? Current Path set is : /usr/bin/java

I receive above error on my server.
While i try to use which JAVA on my server SSH, it return /usr/bin/java

Wonder will still showing this error?
Anyone could advise?

multi lines content only get first line text

output example:

array:20 [▼
  0 => "file:///D:/www/tp6/public//images/test7.jpg (format: QR_CODE, type: ADDRESSBOOK):"
  1 => "Raw result:"
  2 => "BEGIN:VCARD"
  3 => "VERSION:3.0"
  4 => "FN:huang"
  5 => "TEL:13913942792"
  6 => "EMAIL:[email protected]"
  7 => b"ADR:ÏÃÃż¯ÃÀ"
  8 => "END:VCARD"
  9 => "Parsed result:"
  10 => "huang"
  11 => b"ÏÃÃż¯ÃÀ"
  12 => "13913942792"
  13 => "[email protected]"
  14 => "Found 4 result points."
  15 => "  Point 0: (345.5,293.5)"
  16 => "  Point 1: (345.5,104.5)"
  17 => "  Point 2: (534.5,104.5)"
  18 => "  Point 3: (521.0,280.0)"
  19 => ""
]

PHPZxingDecoder.php function createImages set $imageValue = $output[$key+2]; will only get first line text

Cyrillic format

Hi, help please, I have a question on php-zxing, how to do it as a result of executing java code, a result that in Cyrillic format, was displayed normally, and not question marks. thank

Update a Packagist

Hello Siddharth Deshpande, how are you?

Could you update the version that is in Packagist? the version via Composer is still old.

Thank you very much

Method not found, then exception not thrown

Hello!!!

Thanks for this very useful code!

When there is no data per se, there's an uncaught error: for example:

Uncaught Error: Call to undefined method PHPZxing\ZxingBarNotFound::getimageValue()

Please catch it and throw as needed

Thanks again

How to add library into Laravel project

Hi

I have install dsiddharth2/php-zxing into laravel project.

After that I write sample code

<?php
    use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$data           = $decoder->decode('https://internationalbarcodes.com/wp-content/uploads/sites/95/2013/09/07-EAN-13-Standard-1.jpg');
if($data instanceof PHPZxing\ZxingImage) {
print_r($data);
}
?>

and it give error File/Folder does not exist.

I think I miss this step

{  
    "require": {
        "dsiddharth2/php-zxing": "1.0.1"
    }  
}

Where I have to add above line in laravel project.

Cannot detect if it was successful or not

The functions for determining if there was an error do not work if there was not an error, and you get a fatal error for calling "getErrorMessage/getErrorCode". These should work either way and just not return anything.

Likewise, you cannot call the successful messages if there was an error. So I cannot check to see if no bar code was returned to know to show the error either.

Error Interface

I have this error can you please help me
PHP Fatal error: Interface 'PHPZxing\PHPZxingInterface' not found in C:\inetpub\wwwroot\saude\dw\PHPZxing\ZxingBarNotFound.php on line 41

line 41 class ZxingBarNotFound implements PHPZxingInterface i got same error on all phppages

another at example.php

PHP Fatal error: Uncaught Error: Call to a member function isFound() on null in C:\inetpub\wwwroot\saude\dw\examples\example.php:50
Stack trace:
#0 {main}
thrown in C:\inetpub\wwwroot\saude\dw\examples\example.php on line 50

Can you please help me

best regards

Thank you

Windows Usage

After a lot of test, I finally succeeded to decode work.

Here's the solution
$decoder = new \PHPZxing\PHPZxingDecoder();
// Set java path with double quote '"path/to/java/exe"'
$decoder->setJavaPath('"C:\Program Files\Java\jdk1.8.0_202\bin\java.exe"');
// replace \ by / in file path
$newPath=str_replace('\','/',$tmpfilePath);
// add file:/// before file Path
$data = $decoder->decode('file:///'.$newPath);

not work

/usr/bin/java -cp /download/php-zxing/src/bin/javase-3.3.3.jar:/download/php-zxing/src/bin/core-3.3.3.jar:/download/php-zxing/src/bin/jcommander-1.72.jar com.google.zxing.client.j2se.CommandLineRunner php-zxing/src/images/no_bar_code_found.jpeg

i do this command。
then cpu100%,after 60s timeout。

Images within a folder do not work

D:\xampp\htdocs\php-zxing\src>"C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe" -cp D:\xampp\htdocs\php-zxing\src\bin\javase-3.3.3.jar;D:\xampp\htdocs\php-zxing\src\bin\core-3.3.3.jar;D:\xampp\htdocs\php-zxing\src\bin\jcommander-1.72.jar com.google.zxing.client.j2se.CommandLineRunner --recursive D:\xampp\htdocs\php-zxing\src\images

Code like above gives,

Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical at sun.nio.fs.WindowsUriSupport.fromUri(Unknown Source) at sun.nio.fs.WindowsFileSystemProvider.getPath(Unknown Source) at java.nio.file.Paths.get(Unknown Source) at com.google.zxing.client.j2se.CommandLineRunner.expand(CommandLineRunner.java:112) at com.google.zxing.client.j2se.CommandLineRunner.main(CommandLineRunner.java:76)

no output from exec in prepareSingleImage method - PHPZxingDecoder.php

File : PHPZxingDecoder.php
Line number : 161

exec($command, $script_output);

In the above code, $script_output doesn't contain anything when the barcode from image is successfully found. But when the barcode is not found or not parsed, This variable contains file://filepath : No barcode found.

Running the same command in shell logs output of barcode data. Same command when executed in php, $script_output doesn't contain anything.

Thanks.

Cannot detect 1D Code39

I'm not sure if this is because it was not programmed to, or if there is an issue. But it cannot detect a Code39 1D bar code.

decoder warning

Im sorry if there are mistakes.

PHPZxingDecoder.php, line 186
Counting of non-countable objects
In PHP7.2, calling count() with a parameter that is a scalar, null, or an object that doesn't implement Countable.
Do you have any plans to solve this problem?

"Is the java PATH set correctly ?" problem.

I can run the Jar files directly from my SSH command line.

java -cp javase-3.3.3.jar:core-3.3.3.jar:jcommander-1.72.jar com.google.zxing.client.j2se.CommandLineRunner ../../../../../images/testimage.jpg
file:///home/earnrnet/public_html/barcode/vendor/dsiddharth2/php-zxing/src/bin/../../../../../images/testimage.jpg (format: PDF_417, type: TEXT):
Raw result:
6200868213185368
Parsed result:
6200868213185368
Found 8 result points.
  Point 2: (599.0,673.0)
  Point 3: (599.0,754.0)
  Point 6: (534.0,673.0)
  Point 7: (534.0,754.0)

This is my response when I run the PHP file:
Is the java PATH set correctly ? Current Path set is : /home/earnrnet/java/jdk-17.0.1/bin/java"

This is my Java version:

java 17.0.1 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

PATH has been setup and I can use java command within my SSH console.

which java command returns:

~/java/jdk-17.0.1/bin/java

readlink -f returns

/home/earnrnet/java/jdk-17.0.1/bin/java

I have tried the$realJavaPath = exec('readlink -f [FILEPATH]'); with no luck.

I also attempted to edit PHPZxingDecoder.php basePrepare() method from $this->getJavaPath() to java and it still won't work, throwing the same exact error.

Remove XML format when Decoding

I am decoding a bar code that is in XML format and when decoding it removes the <> plus what is inside the <>, what can be done?

Thank you!

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.