PDF to bitmap image conversoin using mupdf n android

淺唱寂寞╮ 提交于 2019-12-10 14:31:39

问题


I'm using mupdf library in my android application to view the pdf files. Can anyone tell me how to get the bitmap images of each page of a pdf using mupdf library? Thanks in advance....


回答1:


use function in MUPDFcore.class,it is called drawPage(int page, int PDF width,int PDF height, 0,0,int bitmap width,int bitmap height)

This function return bitmap image. 1st parameter is the page that will be rendered.

The 2nd and 3rd parameter are the size of PDF.

The 4th and 5th parameter are the beginning of the bitmap position to be filled with PDF rendered image (this is assumption, because there is no exact documentation regarding these parameters)

THe 6th and 7th parameter are the bitmap size that will be filled with PDF rendered image.

I have already done it within the sample project given by them. Now I'm trying to use it in another project but I still have difficulties.




回答2:


I found the solution for generate bitmap.

ThumbnailsActivity.mBitmapList=new ArrayList<Bitmap>();
                for(int i=0;i<core.countPages();i++){
                     Bitmap bitmap=core.drawPage(i, 200, 200, 0, 0, 200, 200);
                     if(bitmap!=null){
                         ThumbnailsActivity.mBitmapList.add(bitmap);
                     }
                }

I hope this may help others.Thanks!




回答3:


The library seems to be updated and doesn't render images if called drawPage() but works fine if we give updatePage()

Find snippet below from the sample source code

//Activity onCreate()

int x = Utils.getScreenSize(this)[0];
int y = Utils.getScreenSize(this)[1];

final ImageView imageView = (ImageView) findViewById(R.id.holderimageview); 
final Bitmap mSharedHqBm = Bitmap.createBitmap(x,y, Bitmap.Config.ARGB_8888);

new CancellableAsyncTask<Void, Void>(getDrawPageTask(mSharedHqBm, x,y, 0, 0, x, y)) {

        @Override
        public void onPreExecute() {
            imageView.setImageBitmap(null);
            imageView.invalidate();

            // Show some imageholder/spinner/progress etc.
        }

        @Override
        public void onPostExecute(Void result) {
            imageView.setImageBitmap(mSharedHqBm);
            imageView.invalidate();
        }
    }

// method in activity
protected CancellableTaskDefinition<Void, Void> getDrawPageTask(final Bitmap bm, final int sizeX, final int sizeY, final int patchX, final int patchY, final int patchWidth, final int patchHeight) { return new MuPDFCancellableTaskDefinition<Void, Void>(core) {
        @Override
        public Void doInBackground(MuPDFCore.Cookie cookie, Void ... params) {
            // Workaround bug in Android Honeycomb 3.x, where the bitmap generation count
            // is not incremented when drawing.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB &&                       Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)                 bm.eraseColor(0);
            core.updatePage(bm, somepagenumber, sizeX, sizeY, patchX, patchY, patchWidth, patchHeight, cookie);
            return null;
            }
        };
}


来源:https://stackoverflow.com/questions/13738091/pdf-to-bitmap-image-conversoin-using-mupdf-n-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!