2013-05-16 42 views
2

我的当前代码写入一个qr代码,但它只是用qr代码写入我的文件。我不确定如何调整要放在文档的一个角落的qr代码的大小,而不是占用整个页面。也不知道RasterImage.Create是否意味着它只用qr创建一个新文件并丢弃我的原始文件?LEADTOOLS将QRBarcode添加到现有图像

代码: - PDF转换为BMP添加QR然后保存回PDF

public void PDFFileExample() 
    { 
     RasterCodecs codecs1 = new RasterCodecs(); 

     codecs1.Options.Pdf.InitialPath = @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32"; 
     codecs1.Dispose(); 
     RasterCodecs codecs2 = new RasterCodecs(); 
     codecs2.ThrowExceptionsOnInvalidImages = true; 
     System.Diagnostics.Debug.Assert(codecs2.Options.Pdf.InitialPath == @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32"); 

     string pdfFile = @"C:\QRCodeTesting\bottomRight.pdf"; 
     string destFileName1 = @"C:\QRCodeTesting\bottomRightOutputTemp.pdf"; 
     string destFileName2 = @"C:\QRCodeTesting\bottomRightOutput.bmp"; 

     RasterCodecs codecs = new RasterCodecs(); 

     if (codecs.Options.Pdf.IsEngineInstalled) 
     { 
      // Resulting image pixel depth. 
      codecs.Options.Pdf.Load.DisplayDepth = 24; 
      codecs.Options.Pdf.Load.GraphicsAlpha = 4; 
      codecs.Options.Pdf.Load.Password = ""; 

      // Type of font anti-aliasing to use. 
      codecs.Options.Pdf.Load.TextAlpha = 1; 
      codecs.Options.Pdf.Load.UseLibFonts = true; 

      // Horizontal,vertical display resolution in dots per inch. 
      codecs.Options.RasterizeDocument.Load.XResolution = 150; 
      codecs.Options.RasterizeDocument.Load.YResolution = 150; 

      using (RasterImage image = codecs.Load(pdfFile, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)) 
      { 

       // Set the PDF version to be v1.4 
       codecs.Options.Pdf.Save.Version = CodecsRasterPdfVersion.V14; 

       try 
       { 
        // Save the image back as PDF 
        codecs.Save(image, destFileName1, RasterImageFormat.RasPdf, 24); 
       } 
       catch (RasterException ex) 
       { 
        if (ex.Code == RasterExceptionCode.FileFormat) 
         MessageBox.Show(string.Format("Image in file {0} is loaded", destFileName1)); 
        else 
        { 
         MessageBox.Show(string.Format("Could not load the file {0}.{1}{2}", destFileName1, Environment.NewLine, ex.Message)); 
        } 
       } 
      } 

      // And load it back before saving it as BMP 
      using (RasterImage image = codecs.Load(destFileName1)) 
      { 
       codecs.Save(image, destFileName2, RasterImageFormat.Bmp, image.BitsPerPixel); 
       writeQRTag(destFileName2); 
      } 
     } 
     else 
     { 
      MessageBox.Show("PDF Engine is not found!"); 
     } 

     // Clean up 
     codecs.Dispose(); 

    } 

QRCode的写作方法

private void writeQRTag(string imageFileName) 
    { 
     BarcodeEngine engine = new BarcodeEngine(); 

     // Create the image to write the barcodes to 
     int resolution = 300; 
     using (RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.Red))) 
     { 
      // Write two QR barcodes 
      WriteQRCode(engine.Writer, image, QRBarcodeSymbolModel.Model1AutoSize, "QR Data 1", true); 

      // Save the image 
      using (RasterCodecs codecs = new RasterCodecs()) 
      { 
       codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1); 
      } 
     } 
    } 

回答

2

这是马从LEADTOOLS支持。

我检查你的代码,发现了以下内容:

1)当你调用RasterImage.Create()方法,它会创建一个包含一个空的红图像,你随后传递给writeQRTag新RasterImage对象()函数,然后使用给定的文件名保存。 保存时,红色将被黑色替换,因为您使用的文件格式只支持黑白。由于您使用的是新图像,因此旧图像会丢失(覆盖)。

如果你想从原始文件写入图像的条形码,你不应该创建一个新的图像。相反,您需要使用已使用codecs.Load()加载的图像并在其上写入条形码。

2)代码执行多个加载和保存操作。通常情况下,除非应用程序需要不同的文件格式(PDF,BMP和TIFF),否则不需要这么做。

3)您创建了我们的RasterCodecs对象的不同实例,但实际只使用其中的一个。代码中不需要4个RasterCodecs对象中的3个。

如果您仍然遇到使用我们工具包的代码的问题,您可以通过电子邮件将我们的详细信息发送给[email protected],我们会尽力为您提供帮助。

+0

我为您创建了一个LEADTOOLS-SDK标签。您可能需要确保将其编辑到您找到的LEADTOOLS问题中,并且您可能想要编辑标记Wiki以获取更多信息。 –