dsiddharth2 / php-zxing

PHP wrapper for Zxing Java library
MIT License
126 stars 25 forks source link

multi lines content only get first line text #21

Open hbh112233abc opened 4 years ago

hbh112233abc commented 4 years ago

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:hbh112233abc@163.com"
  7 => b"ADR:ÏÃÃż¯ÃÀ"
  8 => "END:VCARD"
  9 => "Parsed result:"
  10 => "huang"
  11 => b"ÏÃÃż¯ÃÀ"
  12 => "13913942792"
  13 => "hbh112233abc@163.com"
  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

hbh112233abc commented 4 years ago

I suggest createImages change as below code:

private function createImages($output)
    {
        $image = array();
        $valueStartIndex = null;
        $valueEndIndex = null;
        foreach ($output as $key => $singleLine) {
            if (preg_match('/\(format/', $singleLine)) {
                $imageInfo  = $singleLine;
                $startPos   = strpos($imageInfo, "(") + 1;
                $endPos     = strpos($imageInfo, ")");
                $dataStr   = substr($imageInfo, $startPos, $endPos - $startPos);

                $dataExplode    = explode(",", $dataStr);
                $contentFormat  = explode(":", $dataExplode[0]);
                $format         = $contentFormat[1];
                $contentFormat  = explode(":", $dataExplode[1]);
                $type         = $contentFormat[1];

                $valueStartIndex = $key + 2;

                $exploded = explode(" ", $singleLine);
                $imagePath = array_shift($exploded);
            } else if (preg_match('/No barcode found/', $singleLine)) {
                $exploded = explode(" ", $singleLine);
                $imagePath = array_shift($exploded);
                $image[] = new ZxingBarNotFound($imagePath, 101, "No barcode found");
            } else if (preg_match('/Parsed result/', $singleLine)) {
                $valueEndIndex = $key;
                $valueArray = array_slice($output, $valueStartIndex, $valueEndIndex - $valueStartIndex);
                $imageValue = implode(' ', $valueArray);
                $image[] = new ZxingImage($imagePath, $imageValue, $format, $type);
            }
        }

        return $image;
    }

get imageValue from every Raw result to Parsed result, and use blank space join multi line text