Using muPDF with curl/flip effect

前端 未结 2 447
悲&欢浪女
悲&欢浪女 2020-12-16 17:49

I\'m using muPDF for reading PDFs in my application. I don\'t like its default animation (Switching horizontally). In other side i found this brilliant library for curl effe

相关标签:
2条回答
  • 2020-12-16 18:00

    If the muPDF does not support rendering to a bitmap, you have no other choice than rendering to a regular view and take a screen dump to a bitmap like this:

    View content = findViewById(R.id.yourPdfView);
    Bitmap bitmap = content.getDrawingCache();
    

    Then use this bitmap as input to your other library.

    0 讨论(0)
  • 2020-12-16 18:11

    Where should i get pages one by one and converting them to bitmaps?

    In our application (newspaper app) we use MuPDF to render PDFs. The workflow goes like this:

    1. Download PDF file (we have one PDF per newspaper page)
    2. Render it with MuPDF
    3. Save the bitmap to the filesystem
    4. Load the Bitmap from filesystem as background image to a view

    So, finally, what we use is MuPDFCore.java and its methods drawPage(...) and onDestroy()

    Is this what you want to know or do i miss the point?

    EDIT

    1.) I think it is not necessary to post code how to download a file. But after downloading i add a RenderTask (extends from Runnable) to a Renderqueue and trigger that queue. The RenderTask needs some information for rendering:

    /**
     * constructs a new RenderTask instance
     * @param context: you need Context for MuPdfCore instance
     * @param pageNumber
     * @param pathToPdf 
     * @param renderCallback: callback to set bitmap to the view after    
     * rendering
     * @param heightOfRenderedBitmap: this is the target height
     * @param widthOfRenderedBitmap: this is the target width
     */
    public RenderTask (Context context, Integer pageNumber, String pathToPdf, IRenderCallback,    
                       renderCallback, int heightOfRenderedBitmap, 
                       int widthOfRenderedBitmap) {
    
        //store things in fields
    }
    

    2.) + 3.) The Renderqueue wraps the RenderTask in a new Thread and starts it. So the run-method of the RenderTask will be invoked:

    @Override
    public void run () {
    
        //do not render it if file exists
        if (exists () == true) {
    
            finish();
            return;
        }
    
    
        Bitmap bitmap = render();
    
        //if something went wrong, we can't store the bitmap
        if (bitmap == null) {
    
            finish();
            return;
        }
    
        //now save the bitmap
        // in my case i save the destination path in a String field
        imagePath = save(bitmap, new File("path/to/your/destination/folder/" + pageNumber + ".jpg"));
    
        bitmap.recycle();
        finish();
    }
    
    /**
     * let's trigger the callback
     */
    private void finish () {
    
        if (renderCallback != null) {
    
            // i send the whole Rendertask to callback
            // maybe in your case it is enough to send the pageNumber or path to    
            // renderend bitmap   
            renderCallback.finished(this); 
        }
    
    }
    
    /**
     * renders a bitmap
     * @return
     */
    private Bitmap render() {
    
        MuPDFCore core = null;
        try {
            core = new MuPDFCore(context, pathToPdf);
        } catch (Exception e) {
    
            return null;
        }
    
        Bitmap bm = Bitmap.createBitmap(widthOfRenderedBitmap, heightOfRenderedBitmap, Config.ARGB_8888);
        // here you render the WHOLE pdf cause patch-x/-y == 0
        core.drawPage(bm, 0, widthOfRenderedBitmap, heightOfRenderedBitmap, 0, 0, widthOfRenderedBitmap, heightOfRenderedBitmap, core.new Cookie());
        core.onDestroy();
        core = null;
        return bm;
    }
    
    /**
     * saves bitmap to filesystem
     * @param bitmap
     * @param image
     * @return
     */
    private String save(Bitmap bitmap, File image) {
    
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(image.getAbsolutePath());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
            return image.getAbsolutePath();
    
        } catch (Exception e) {
    
            return null;
        }
    
        finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch(Throwable ignore) {}
    
        }
    }  
    

    }

    4.) I think it is not necessary to post code how to set a bitmap as background of a view

    0 讨论(0)
提交回复
热议问题