Showing
4 changed files
with
53 additions
and
7 deletions
| @@ -62,6 +62,51 @@ class PdfImage extends PdfXObject { | @@ -62,6 +62,51 @@ class PdfImage extends PdfXObject { | ||
| 62 | } | 62 | } |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | + factory PdfImage.jpeg(PdfDocument pdfDocument, {@required Uint8List image}) { | ||
| 66 | + assert(image != null); | ||
| 67 | + | ||
| 68 | + int width; | ||
| 69 | + int height; | ||
| 70 | + int offset = 0; | ||
| 71 | + while (offset < image.length) { | ||
| 72 | + while (image[offset] == 0xff) { | ||
| 73 | + offset++; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + final int mrkr = image[offset]; | ||
| 77 | + offset++; | ||
| 78 | + | ||
| 79 | + if (mrkr == 0xd8) { | ||
| 80 | + continue; // SOI | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + if (mrkr == 0xd9) { | ||
| 84 | + break; // EOI | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + if (0xd0 <= mrkr && mrkr <= 0xd7) { | ||
| 88 | + continue; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + if (mrkr == 0x01) { | ||
| 92 | + continue; // TEM | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + final int len = (image[offset] << 8) | image[offset + 1]; | ||
| 96 | + offset += 2; | ||
| 97 | + | ||
| 98 | + if (mrkr == 0xc0) { | ||
| 99 | + height = (image[offset + 1] << 8) | image[offset + 2]; | ||
| 100 | + width = (image[offset + 3] << 8) | image[offset + 4]; | ||
| 101 | + break; | ||
| 102 | + } | ||
| 103 | + offset += len - 2; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + return PdfImage(pdfDocument, | ||
| 107 | + image: image, width: width, height: height, jpeg: true, alpha: false); | ||
| 108 | + } | ||
| 109 | + | ||
| 65 | /// RGBA Image Data | 110 | /// RGBA Image Data |
| 66 | final Uint8List image; | 111 | final Uint8List image; |
| 67 | 112 |
| @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl | @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl | ||
| 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf | 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf |
| 5 | repository: https://github.com/DavBfr/dart_pdf | 5 | repository: https://github.com/DavBfr/dart_pdf |
| 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues | 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues |
| 7 | -version: 1.3.7 | 7 | +version: 1.3.8 |
| 8 | 8 | ||
| 9 | environment: | 9 | environment: |
| 10 | sdk: ">=2.1.0 <3.0.0" | 10 | sdk: ">=2.1.0 <3.0.0" |
| @@ -36,12 +36,10 @@ void main() { | @@ -36,12 +36,10 @@ void main() { | ||
| 36 | final PdfDocument pdf = PdfDocument(); | 36 | final PdfDocument pdf = PdfDocument(); |
| 37 | final PdfPage page = PdfPage(pdf, pageFormat: PdfPageFormat.a4); | 37 | final PdfPage page = PdfPage(pdf, pageFormat: PdfPageFormat.a4); |
| 38 | 38 | ||
| 39 | - final PdfImage image = PdfImage(pdf, | ||
| 40 | - image: await download('https://www.nfet.net/nfet.jpg'), | ||
| 41 | - width: 472, | ||
| 42 | - height: 477, | ||
| 43 | - jpeg: true, | ||
| 44 | - alpha: false); | 39 | + final PdfImage image = PdfImage.jpeg( |
| 40 | + pdf, | ||
| 41 | + image: await download('https://www.nfet.net/nfet.jpg'), | ||
| 42 | + ); | ||
| 45 | 43 | ||
| 46 | final PdfGraphics g = page.getGraphics(); | 44 | final PdfGraphics g = page.getGraphics(); |
| 47 | g.drawImage(image, 30, page.pageFormat.height - 507.0); | 45 | g.drawImage(image, 30, page.pageFormat.height - 507.0); |
-
Please register or login to post a comment