jblough / Android-Pdf-Viewer-Library

DEPRECATED - Pdf Viewer library for Android
657 stars 329 forks source link

Image rendering in preICS is good but not in ICS and up. #14

Open stjom opened 11 years ago

stjom commented 11 years ago

The image rendering capability of library works fine with Gingerbread OS and lower but not in ICS and up.

ndorigatti commented 11 years ago

same for me, I was talking with a colleague, we think it is due to some wrong (or different) management of Images from ICS and above. I'm going to export the page to bmp (instead of png) so I can understand and get the values for each pixel, then i will try to debug in both froyo and jellybean. I think it will take weeks (i have a few hours per week). If anyone wants to help, we can discuss, i think the way for understand the error is the above one.

ndorigatti commented 11 years ago

After some tests and debugging I found some hints: In DCTDecode Class, at line 81 (in my code) -> Config conf = img.getConfig(); the images are ok in both Gingerbread and JellyBean. Then i checked what happens in PDFImage->parseData and at line 264 (the condition for COLORSPACE RGB) i found that the byte[] is different in ICS and UP than in GB and below. It is exactly 2times, so we need to manage the "for" cycle taking it into account. Honestly I'm not so good at parsing and managing Hex data, so I'll try to keep on, but if someone has skills, please help!

ndorigatti commented 11 years ago

I finally found a solution (which I have to test better). The problem is (it's all my idea, so I'm not sure), related to the "doubled" array size. I managed to make it work by removing the big difficult 'for' and replace it with a simple: bi.copyPixelsFromBuffer(ByteBuffer.wrap(imgBytes)); then the images are created correctly! :dancer: :+1:

munagasanjeev commented 11 years ago

Nick.Thank you for update.I tried it now the images are looking great.But there are some other problems like 1) some times text not coming properly 2) when try to zoom in and zoom out data is missing 3) some times text is coming bold continuously even though text is normal in pdf.

I am ready to help you to solve these problems.please guide me how to solve these

ndorigatti commented 11 years ago

Hello! Fine that images are ok for you too (I have only few pdf for testing and all follow the same "schema"). About text I have too some problems, like some text have background black behind the whole text (like if it is marked), and this is due to the missing implementation of PDFShader. I do have a desktop implementation of PDFShader that should be converted to Android, but I don't know how much could it take to do. I've never dig into the real implementation.

About your points, can you show me differences between pdf and exported image? I'm not using Activity or Views for showing PDFs, I just run a background task that converts PDF to PNG.

The PDFShader desktop class comes from an optimized version of the "PDFRenderer" library and is on GitHub, but unfortunately I forgot to "Star" the project so we need to search for it!

munagasanjeev commented 11 years ago

pdf app pdfimage2 app2

munagasanjeev commented 11 years ago

please look the above pics 1 and 3 are pdf images.2 and 4 are images taken from my app from jellybean device.

In 2 image there is unknown text ,ovvou.ooov is comint in 4 image there is black border to the image which is not there in pdf

munagasanjeev commented 11 years ago

app3 pdf34 Also some times images are missing.In this case most of the times.

ndorigatti commented 11 years ago

ok, pic4 should be related to the shader problem (is the same i have with shaded texts). about problem in pic2, I'cant understand what is, it's strange... do it happens only on JellyBean? did you try on emulator?

ndorigatti commented 11 years ago

just to be sure, did you check in logcat for "OutOfMemory" exceptions? Hope is not that but missing image could be that problem. We should also check which colorspace and decoding is used for that two images, because it could be a case that falls out of the fix made yesterday

munagasanjeev commented 11 years ago

Actually there are two exceptions

java.nio.BufferUnderflowException java.nio.Buffer.checkGetBounds(Buffer.java:177) java.nio.DirectByteBuffer.get(DirectByteBuffer.java:41) at java.nio.MappedByteBufferAdapter.get(MappedByteBufferAdapter.java:144) at java.nio.ByteBuffer.get(ByteBuffer.java:384) at net.sf.andpdf.nio.NioByteBuffer.get(NioByteBuffer.java:99) at com.sun.pdfview.decode.FlateDecode.decode(FlateDecode.java:68) at com.sun.pdfview.decode.PDFDecoder.decodeStream(PDFDecoder.java:92) at com.sun.pdfview.PDFObject.decodeStream(PDFObject.java:334) at com.sun.pdfview.PDFObject.getStreamBuffer(PDFObject.java:307) com.sun.pdfview.PDFObject.getStreamBuffer(PDFObject.java:304) at com.sun.pdfview.font.TTFFont.(TTFFont.java:80) at com.sun.pdfview.font.CIDFontType2.(CIDFontType2.java:70) at com.sun.pdfview.font.PDFFont.getFont(PDFFont.java:174) at com.sun.pdfview.font.Type0Font.(Type0Font.java:51) at com.sun.pdfview.font.PDFFont.getFont(PDFFont.java:145) at com.sun.pdfview.PDFParser.getFontFrom(PDFParser.java:1349) at com.sun.pdfview.PDFParser.iterate(PDFParser.java:848) at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101) at com.sun.pdfview.BaseWatchable.execute(BaseWatchable.java:263) at com.sun.pdfview.BaseWatchable.go(BaseWatchable.java:197) at com.sun.pdfview.PDFFile.getPage(PDFFile.java:1589) at net.sf.andpdf.pdfviewer.PdfViewerActivity.showPage(PdfViewerActivity.java:779) at net.sf.andpdf.pdfviewer.PdfViewerActivity.access$12(PdfViewerActivity.java:769) at net.sf.andpdf.pdfviewer.PdfViewerActivity$3.run(PdfViewerActivity.java:259) at java.lang.Thread.run(Thread.java:864)

java.lang.ArrayIndexOutOfBoundsException: length=3874; index=17010 at com.sun.pdfview.font.ttf.GlyfTable.getGlyph(GlyfTable.java:57) at com.sun.pdfview.font.TTFFont.getOutline(TTFFont.java:175) at com.sun.pdfview.font.CIDFontType2.getOutline(CIDFontType2.java:272) at com.sun.pdfview.font.OutlineFont.getGlyph(OutlineFont.java:131) at com.sun.pdfview.font.PDFFont.getCachedGlyph(PDFFont.java:310) at com.sun.pdfview.font.PDFFontEncoding.getGlyphFromCMap(PDFFontEncoding.java:155) at com.sun.pdfview.font.PDFFontEncoding.getGlyphs(PDFFontEncoding.java:115) at com.sun.pdfview.font.PDFFont.getGlyphs(PDFFont.java:276) at com.sun.pdfview.PDFTextFormat.doTextNormal(PDFTextFormat.java:324) at com.sun.pdfview.PDFTextFormat.doText(PDFTextFormat.java:271) at com.sun.pdfview.PDFTextFormat.doText(PDFTextFormat.java:356) at com.sun.pdfview.PDFParser.iterate(PDFParser.java:904) at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101) at com.sun.pdfview.BaseWatchable.execute(BaseWatchable.java:263) at com.sun.pdfview.BaseWatchable.go(BaseWatchable.java:197) at com.sun.pdfview.PDFFile.getPage(PDFFile.java:1589) at net.sf.andpdf.pdfviewer.PdfViewerActivity.showPage(PdfViewerActivity.java:779) at net.sf.andpdf.pdfviewer.PdfViewerActivity.access$12(PdfViewerActivity.java:769) at net.sf.andpdf.pdfviewer.PdfViewerActivity$3.run(PdfViewerActivity.java:259) at java.lang.Thread.run(Thread.java:864)

ndorigatti commented 11 years ago

ok, for flat decode check if you have the fixes of this class: https://github.com/preichelt/Android-Pdf-Viewer-Library/blob/master/src/com/sun/pdfview/decode/FlateDecode.java Looking at your error line, I think you dont (there is an issue with explained all the classes for fixing the issue).

The second one is bigger to find, I'm at work and I don't have my implementation, if your pdf is not "personal" try to send it to me, I'll check if it works (because I've merged some stuff but never tested, so I don't want to post "beta" solutions).

munagasanjeev commented 11 years ago

i can't attach pdf here please tell me your mail or some other way to send pdf.Thank you for your help.Since i can't use either commercial or GPL libraries.This library helps a lot.Thank you very much man.

ndorigatti commented 11 years ago

send to "turricatch" and use gmail commercial domain. I'll try to do today or tomorrow but I have short time, for sure i can do it on Tuesday

munagasanjeev commented 11 years ago

all the problems solved but some times images are missing. getting below exception when ever images are missing

java.lang.RuntimeException: Buffer not large enough for pixels at android.graphics.Bitmap.copyPixelsFromBuffer(Bitmap.java:692) at com.sun.pdfview.PDFImage.parseData(PDFImage.java:275) at com.sun.pdfview.PDFImage.getImage(PDFImage.java:225) at com.sun.pdfview.PDFRenderer.drawImage(PDFRenderer.java:374) at com.sun.pdfview.PDFImageCmd.execute(PDFPage.java:654) at com.sun.pdfview.PDFRenderer.iterate(PDFRenderer.java:674) at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101) at com.sun.pdfview.BaseWatchable.execute(BaseWatchable.java:263) at com.sun.pdfview.BaseWatchable.go(BaseWatchable.java:197) at com.sun.pdfview.PDFPage.getImage(PDFPage.java:237) at net.sf.andpdf.pdfviewer.PdfViewerActivity.showPage(PdfViewerActivity.java:790) at net.sf.andpdf.pdfviewer.PdfViewerActivity.access$12(PdfViewerActivity.java:769) at net.sf.andpdf.pdfviewer.PdfViewerActivity$3.run(PdfViewerActivity.java:259) at java.lang.Thread.run(Thread.java:864)

please let me know ,how to solve this

ndorigatti commented 11 years ago

The problem of "Buffer not large enough for pixels" derives from the fix previously mentioned: https://github.com/jblough/Android-Pdf-Viewer-Library/issues/14#issuecomment-14935327 when the image is decoded as "3 byte per pixel" the copyPixelFromBuffer does not work. I've made a fix which manually adds pixel values, here the problem is that transparency is missing. It happens when images are decoded as "FlateDecode" an is probably due to the absence of a way to understand which pixels are invisible. I temporary set black and white pixels to invisible, but for sure is not correct.

munagasanjeev commented 11 years ago

Thanks nick .please give me some guidance to fix that.

ndorigatti commented 11 years ago

you already have fixes, the others don't! :+1:

munagasanjeev commented 11 years ago

as you mentioned earlier to me that this is temparory fix ??

On Fri, Mar 22, 2013 at 7:30 PM, Nicola Dorigatti notifications@github.comwrote:

you already have fixes, the others don't! [image: :+1:]

— Reply to this email directly or view it on GitHubhttps://github.com/jblough/Android-Pdf-Viewer-Library/issues/14#issuecomment-15297931 .

ndorigatti commented 11 years ago

is temporary because i don't think is the rigth solution, the right one should be to have the image mask, but i don't know why i can't get it. On desktop version image is ok, but i have no time to dig into that. I'm adding support for shading that (to me) has the priority

munagasanjeev commented 11 years ago

please let me know, once u have implemented .thank you

On Fri, Mar 22, 2013 at 7:38 PM, Nicola Dorigatti notifications@github.comwrote:

is temporary because i don't think is the rigth solution, the right one should be to have the image mask, but i don't know why i can't get it. On desktop version image is ok, but i have no time to dig into that. I'm adding support for shading that (to me) has the priority

— Reply to this email directly or view it on GitHubhttps://github.com/jblough/Android-Pdf-Viewer-Library/issues/14#issuecomment-15298366 .

ndorigatti commented 11 years ago

I have definetly gave up on this project. I've hope to have some help and discussion with other guys but it seems noone cares about this project. I've found the background of images has nothing to do with Patterns and Shaders, i tryed to port PatternType2 and ShaderType2 but there are too much java.awt classes that are not mapped on Android or i can't understand how to map (WritableRaster?). Anyway, the issue on the shaders probably has to do with image mask or font glyph. I have no time and no knowledge for that. If in future someone would proceed on this work, i will keep notification for this discussion. Goodbye

jesperborgstrup commented 11 years ago

Source code for Nick986's fix (I got it to work with this):

PDFImage.java: Change

        } else if (colorSpace.getType() == PDFColorSpace.COLORSPACE_RGB) {
            int maxH = getHeight();
            int maxW = getWidth();
            if (imgBytes.length == 2*maxW*maxH) {
                // decoded JPEG as RGB565
                bi = Bitmap.createBitmap(maxW, maxH, Config.RGB_565);
                bi.copyPixelsFromBuffer(ByteBuffer.wrap(imgBytes));
            }
            else {
                // create RGB image
                bi = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
                int[] line = new int[maxW]; 
                int n=0;
                for (int h = 0; h<maxH; h++) {
                    for (int w = 0; w<getWidth(); w++) {
                        line[w] = ((0xff&(int)imgBytes[n])<<8|(0xff&(int)imgBytes[n+1]))<<8|(0xff&(int)imgBytes[n+2])|0xFF000000;
    //                      line[w] = Color.rgb(0xff&(int)imgBytes[n], 0xff&(int)imgBytes[n+1],0xff&(int)imgBytes[n+2]);
                        n+=3;
                    }
                    bi.setPixels(line, 0, maxW, 0, h, maxW, 1);
                }
            }
        }

to

        } else if (colorSpace.getType() == PDFColorSpace.COLORSPACE_RGB) {
            int maxH = getHeight();
            int maxW = getWidth();
            bi = Bitmap.createBitmap(maxW, maxH, imgBytes.length == 2*maxW*maxH ? Config.RGB_565 : Config.ARGB_8888 );
            bi.copyPixelsFromBuffer(ByteBuffer.wrap(imgBytes));
        }
ndorigatti commented 11 years ago

even better, thanks for the fix

derzu commented 11 years ago

I tried the code change on PDFImage.java that @jesperborgstrup posted (from @Nick986 fix), and it fix my problem with the images. But I still having problems with some parts of text render. Does anyone find a full solution?

agos commented 11 years ago

@derzu no. I gave up since none of the proposed solutions seems to work with CMYK images (or at least that's the reason I think it won't work for me).

StEaLtHmAn commented 11 years ago

I have recompiled the solution and made a pdf viewer. The images are not in the best quality but they seem to work. http://stackoverflow.com/a/16294833/2027232

ndorigatti commented 11 years ago

@StEaLtHmAn sorry but which solution? the one in the link or the ones in this post? where do you recompiled it?

StEaLtHmAn commented 11 years ago

everything is on the link...

derzu commented 11 years ago

I solved my problem using another pdf reader lib, the mupdf (http://www.mupdf.com). It decodes all the parts of the pdf perfectly.

sandeepsaini289 commented 11 years ago

Photos are seen good enough but not text, can anybody tell ow to fix it..??/

StEaLtHmAn commented 11 years ago

have you tried:

PDFPaint.s_doAntiAlias = true;
sandeepsaini289 commented 11 years ago

yes but it wouldn't work...:(

mamini commented 10 years ago

Thanks a lot for your helps and comments. I used @ndorigatti solution for PDFImage.parseData() and it was very helpful. After that I have same problem as @munagasanjeev in rendering some images in pdf. I tried a temporary solution for that. It's not a complete and basic solution, but it's working fine for me! Actually I used both of operations for parsing data in RGB colorspace. I used this in PDFImage.parseData() :

else if (colorSpace.getType() == PDFColorSpace.COLORSPACE_RGB) {
            int maxH = getHeight();
            int maxW = getWidth();
            bi = Bitmap.createBitmap(maxW, maxH, imgBytes.length == 2 * maxW
                    * maxH ? Config.RGB_565 : Config.ARGB_8888);
            try {
                bi.copyPixelsFromBuffer(ByteBuffer.wrap(imgBytes));
            } catch (Exception e) {
                int[] line = new int[maxW];
                int n = 0;
                for (int h = 0; h < maxH; h++) {
                    for (int w = 0; w < getWidth(); w++) {
                        line[w] = ((0xff & (int) imgBytes[n]) << 8 | (0xff & (int) imgBytes[n + 1])) << 8
                                | (0xff & (int) imgBytes[n + 2]) | 0xFF000000;
                         n += 3;
                    }
                    bi.setPixels(line, 0, maxW, 0, h, maxW, 1);
                }
            }
ndorigatti commented 10 years ago

@mamini with this code you do resolve the problem of "Some pictures are not rendered" or do you fix the problem of the Black Corning for images?

Seems ok, you should anyway understand why it throws exception while coping pixel from buffer, because in this case we can make a good fix (try-catch is not the best way...)

mamini commented 10 years ago

I fixed the problem of "Some pictures are not rendered". Yes try-catch is not a good way, I'll try to find a good solution for this problem. Let me know if you found a good solution for this problem.

marquessbr commented 9 years ago

Hi First of all thank you! Im newbie in this so I need to convert a document to images but what I need to do? In my understanding what I need to do would be to compile code as a library in android-studio, or I can simply use the "pdfviewlibrary.jar" that is ready in the source folder?

thank you!

peace!

hamzehnasajpour commented 9 years ago

Hi, I'm using this library. Every thing is ok but I have a problem with pdf that have watermark. For example if I have a pdf with text that have a picture(watermark), the watermark show with 100% transparency on the text. So I could see text because the text is under the background image. with other pdf viewer i haven't this problem. Please guide me.

jorcap commented 9 years ago

Hi, This library works fine and create a perfect bitmap, if the PDF colorspace is RGB. If the PDF contains gray images, and if the colorspace is CYMK, the bitmap contains only text and no images. Anyone can work this around? Thanks