2012-07-05 24 views
5

Tôi cần có thể đọc mã QR từ các tệp PDF. Tôi đang sử dụng thinkworks.QRCode, chấp nhận hình ảnh và trả về dữ liệu được giữ trong mã QR. Tôi có một phần làm việc.Chuyển đổi PDF sang hình ảnh tạm thời với mục đích đọc mã QR

Tuy nhiên, tôi cần phải có thể chấp nhận tệp PDF nhiều trang và gửi từng trang dưới dạng hình ảnh tới trình đọc QR. Sau đó, tôi cần phải lưu từng trang của tệp PDF gốc dưới dạng một trang PDF được đặt tên theo dữ liệu có trong mã QR.

Bạn khuyên tôi nên sử dụng thư viện nào cho dự án này? Nhiều người mà tôi đã thấy tạo ra hình ảnh vĩnh viễn, nhưng tôi chỉ muốn những hình ảnh tạm thời. Có cái gì đó sẽ dễ dàng cho phép tôi làm điều này? Có lẽ một trình đọc QR khác có thể đọc được các tệp pdf không?

Cảm ơn mọi lời khuyên bạn có thể cho vay!

Trả lời

5

Tôi đã sử dụng itextsharp và libtiff.NET để trích xuất hình ảnh tiff từ tệp PDF vào bộ nhớ. Điểm mấu chốt là itextsharp sẽ cung cấp cho bạn quyền truy cập vào các hình ảnh, nhưng nếu chúng được mã hóa, bạn cần tự mã hóa hoặc sử dụng một thư viện khác, đó là nơi libtiff.NET đến.

Mã sau đây đã được sửa đổi dựa trên câu trả lời cho câu hỏi tôi đã hỏi: PDF Add Text and Flatten

Private Shared Function ExtractImages(ByVal pdf As Byte()) As List(Of Byte()) 
    Dim images As New List(Of Byte()) 
    Dim reader As New PdfReader(pdf) 

    If (reader IsNot Nothing) Then 
     ' Loop through all of the references in the PDF. 
     For refIndex = 0 To (reader.XrefSize - 1) 
      ' Get the object. 
      Dim obj = reader.GetPdfObject(refIndex) 

      ' Make sure we have something and that it is a stream. 
      If (obj IsNot Nothing) AndAlso obj.IsStream() Then 
       ' Cast it to a dictionary object. 
       Dim pdfDict = DirectCast(obj, iTextSharp.text.pdf.PdfDictionary) 

       ' See if it has a subtype property that is set to /IMAGE. 
       If pdfDict.Contains(iTextSharp.text.pdf.PdfName.SUBTYPE) AndAlso (pdfDict.Get(iTextSharp.text.pdf.PdfName.SUBTYPE).ToString() = iTextSharp.text.pdf.PdfName.IMAGE.ToString()) Then 
        ' Grab various properties of the image. 
        Dim filter = pdfDict.Get(iTextSharp.text.pdf.PdfName.FILTER).ToString() 
        Dim width = pdfDict.Get(iTextSharp.text.pdf.PdfName.WIDTH).ToString() 
        Dim height = pdfDict.Get(iTextSharp.text.pdf.PdfName.HEIGHT).ToString() 
        Dim bpp = pdfDict.Get(iTextSharp.text.pdf.PdfName.BITSPERCOMPONENT).ToString() 

        ' Grab the raw bytes of the image 
        Dim bytes = PdfReader.GetStreamBytesRaw(DirectCast(obj, PRStream)) 

        ' Images can be encoded in various ways. 
        ' All of our images are encoded with a single filter. 
        ' If there is a need to decode another filter, it will need to be added. 
        If (filter = iTextSharp.text.pdf.PdfName.CCITTFAXDECODE.ToString()) Then 
         Using ms = New MemoryStream() 
          Using tiff As Tiff = tiff.ClientOpen("memory", "w", ms, New TiffStream()) 
           tiff.SetField(TiffTag.IMAGEWIDTH, width) 
           tiff.SetField(TiffTag.IMAGELENGTH, height) 
           tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4) 
           tiff.SetField(TiffTag.BITSPERSAMPLE, bpp) 
           tiff.SetField(TiffTag.SAMPLESPERPIXEL, 1) 

           tiff.WriteRawStrip(0, bytes, bytes.Length) 
           tiff.Flush() 
           images.Add(ms.ToArray()) 
           tiff.Close() 
          End Using 
         End Using 
        Else 
         Throw New NotImplementedException("Decoding this filter has not been implemented") 
        End If 
       End If 
      End If 
     Next 
    End If 

    Return images 
End Function