Skip to content
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

Detect multiple barcodes from pdf #134

Open
itutsjain opened this issue Mar 31, 2024 · 0 comments
Open

Detect multiple barcodes from pdf #134

itutsjain opened this issue Mar 31, 2024 · 0 comments

Comments

@itutsjain
Copy link

`
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant