khoren93 / flutter_zxing

Flutter plugin for scanning and generating QR codes using the ZXing library, supporting Android, iOS, and desktop platforms
https://pub.dev/packages/flutter_zxing
MIT License
88 stars 50 forks source link

Detect multiple barcodes from pdf #134

Open itutsjain opened 3 months ago

itutsjain commented 3 months ago

` void main() { runApp(MyApp()); }

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: ElevatedButton( onPressed: () async { FilePickerResult? result = await FilePicker.platform.pickFiles( type: FileType.custom, allowedExtensions: ['pdf'], );

          if (result != null) {
            File file = File(result.files.single.path!);
            extractImagesFromPath(file.path);
          }
        },
        child: Text('Select PDF'),
      ),
    ),
  ),
);

} }

Future extractImagesFromPath(String pdfPath) async { final doc = await PdfDocument.openFile(pdfPath); final pages = doc.pageCount;

for (int i = 1; i <= pages; i++) { var page = await doc.getPage(i);

var imgPDF = await page.render();
var img = await imgPDF.createImageDetached();
var imgBytes = await img.toByteData(format: ImageByteFormat.png);
var libImage = imglib.decodeImage(imgBytes!.buffer
    .asUint8List(imgBytes.offsetInBytes, imgBytes.lengthInBytes));

final tempDir = await getTemporaryDirectory();
final tempFile = File('${tempDir.path}/temp_image_$i.png');
await tempFile.writeAsBytes(imglib.encodePng(libImage!));

// Read the barcodes from the temporary file
DecodeParams params = DecodeParams(maxSize: 1920); // Set the max size as needed
Codes results = await zx.readBarcodesImagePath(XFile(tempFile.path), params: params);

if (results.codes.isNotEmpty) {
  for (var result in results.codes) {
    if (result.isValid) {
      print('QR Code Data: ${result.text}');
    } else {
      print('No QR code detected');
    }
  }
} else {
  print('No QR codes detected');
}

} }`

I am unable to detect the barcodes, I am using the latest version of flutter-zxing.