-
-
Notifications
You must be signed in to change notification settings - Fork 290
Open
Description
I have a bug when converting tiff files to jpg, the generated jpg has a red background, how can I solve it?
//Dart SDK version: 3.2.6
import 'dart:io';
import 'dart:typed_data';
import 'package:pdf/widgets.dart' as pw; //^3.11.3
import 'package:image/image.dart' as img; //^4.5.4
import 'package:path/path.dart' as p;
void main(List<String> args) async {
final files = [
'scripts/reproduzir_bug/1.TIF',
'scripts/reproduzir_bug/2.TIF',
'scripts/reproduzir_bug/3.TIF'
];
final tiffFile = File(files[0]);
final tiffBytes = await tiffFile.readAsBytes();
final image = img.decodeTiff(tiffBytes);
final jpgBytes =
img.encodeJpg(image!, quality: 70, chroma: img.JpegChroma.yuv420);
await File('scripts/reproduzir_bug/1.jpg').writeAsBytes(jpgBytes);
// final bytes = await convertTiffToPDF(files);
// await File('scripts/reproduzir_bug/output.pdf').writeAsBytes(bytes);
}
Future<Uint8List> convertTiffToPDF(List<String> paths) async {
final pdf = pw.Document();
final supportedExtensions = ['tiff', 'tif'];
for (final imagePath in paths) {
try {
final imageFile = File(imagePath);
if (await imageFile.exists()) {
final extension = p.extension(imagePath).substring(1).toLowerCase();
if (supportedExtensions.contains(extension)) {
final tiffBytes = await imageFile.readAsBytes();
final image = img.decodeTiff(tiffBytes);
if (image == null) {
throw Exception('Falha ao decodificar o arquivo TIFF.');
}
image.backgroundColor = img.ColorRgb8(255, 255, 255);
print('image Channel: ${image.bitsPerChannel}');
// Codifica para PNG
final pngBytes =
img.encodeJpg(image, quality: 70, chroma: img.JpegChroma.yuv420);
final memoryImage = pw.MemoryImage(pngBytes);
pdf.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(child: pw.Image(memoryImage));
},
));
} else {
print('Arquivo não suportado para embedding no PDF: $imagePath');
}
} else {
print('Arquivo não encontrado: $imagePath');
}
} catch (e) {
print('Erro ao processar a imagem $imagePath: $e');
}
}
final file = await pdf.save();
return file;
}
hodfords-tran-ta-mobile
Metadata
Metadata
Assignees
Labels
No labels