Skip to content

Added support for files with multiple barcodes #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "tarfin-labs/zbar-php",
"name": "mehr-it/zbar-php",
"description": "zbar-php is a php package that provides an interface to the zbar bar-code reading library.",
"keywords": [
"tarfin-labs",
Expand Down
52 changes: 50 additions & 2 deletions src/Zbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ private function runProcess()

$this->process->run();

$output = $this->process->getOutput();

if (! $this->process->isSuccessful()) {
throw ZbarError::exitStatus($this->process->getExitCode());

if ($this->process->getExitCode() !== -1 || strpos($output, '<barcodes') !== 0)
throw ZbarError::exitStatus($this->process->getExitCode());
}

return $this->output = $this->parse($this->process->getOutput());
return $this->output = $this->parse($output);
}

/**
Expand All @@ -87,10 +91,31 @@ private function runProcess()
public function scan()
{
$output = $this->runProcess();

if (is_array($output))
$output = $output[0];

return $output->data;
}

/**
* Scan bar-codes and return all values.
*
* @return string[]
*
* @throws \TarfinLabs\ZbarPhp\Exceptions\ZbarError
*/
public function scanAll() {
$output = $this->runProcess();

if (!is_array($output))
$output = [$output];

return array_map(function ($item) {
return $item->data;
}, $output);
}

/**
* Get the bar-code type after scanning it.
*
Expand All @@ -113,11 +138,34 @@ public function type()
public function decode()
{
$output = $this->runProcess();

if (is_array($output))
$output = $output[0];

$code = $output->data;
$type = $output->{'@attributes'}->type;

return new BarCode($code, $type);
}

/**
* Find both the bar-code and type of the bar-codes then returns objects for all barcodes.
*
* @return BarCode[]
*
* @throws \TarfinLabs\ZbarPhp\Exceptions\ZbarError
*/
public function decodeAll()
{
$output = $this->runProcess();

if (!is_array($output))
$output = [$output];

return array_map(function ($item) {
return new BarCode($item->data, $item->{'@attributes'}->type);
}, $output);
}

/**
* Return symbol object.
Expand Down
71 changes: 71 additions & 0 deletions tests/ZbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class ZbarTest extends TestCase
*/
protected $ean13;

/**
* @var string
*/
protected $code128andEan13;

/**
* @var string
*/
Expand All @@ -49,6 +54,7 @@ protected function setUp(): void
$this->emptyImage = __DIR__.'/files/empty.png';
$this->ean13 = __DIR__.'/files/ean-13.jpg';
$this->code128 = __DIR__.'/files/code-128.png';
$this->code128andEan13 = __DIR__.'/files/code-128-and-ean13.png';
}

/** @test */
Expand Down Expand Up @@ -141,4 +147,69 @@ public function it_can_get_bar_code_and_type_of_qrcode()
$this->assertSame('tarfin', $barCode->code());
$this->assertSame('QR-Code', $barCode->type());
}

/** @test */
public function it_can_scan_barcode_from_file_with_multiple_barcodes() {
$zbar = new Zbar($this->code128andEan13);
$code = $zbar->scan();

$this->assertSame(true, in_array($code, ['1234567890', '1234567890128']));
}

/** @test */
public function it_can_scan_all_barcodes_from_file_with_multiple_barcodes() {
$zbar = new Zbar($this->code128andEan13);
$codes = $zbar->scanAll();

$this->assertSame(true, in_array('1234567890', $codes));
$this->assertSame(true, in_array('1234567890128', $codes));
$this->assertCount(2, $codes);
}

/** @test */
public function it_can_get_barcode_and_type_from_file_with_multiple_barcodes() {
$zbar = new Zbar($this->code128andEan13);
$barcode = $zbar->decode();

switch ($barcode->code()) {
case '1234567890128':
$this->assertSame('EAN-13', $barcode->type());
break;
case '1234567890':
$this->assertSame('CODE-128', $barcode->type());
break;
default:
$this->fail("Unexpected barcode value \"{$barcode->code()}\".");
}
}

/** @test */
public function it_can_get_all_barcodes_and_types_from_file_with_multiple_barcodes() {
$zbar = new Zbar($this->code128andEan13);
$barcodes = $zbar->decodeAll();

$seen = [
'1234567890128' => false,
'1234567890' => false,
];

foreach($barcodes as $barcode) {


switch ($barcode->code()) {
case '1234567890128':
$this->assertSame('EAN-13', $barcode->type());
break;
case '1234567890':
$this->assertSame('CODE-128', $barcode->type());
break;
default:
$this->fail("Unexpected barcode value \"{$barcode->code()}\".");
}
$seen[$barcode->code()] = true;
}

$this->assertSame(true, $seen['1234567890128']);
$this->assertSame(true, $seen['1234567890']);
}
}
Binary file added tests/files/code-128-and-ean13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.