1

I am trying to use the VuDroid PDF viewer and I need to take the rendered bitmap and store it as a byte[]. Then I need to convert it back into a Bitmap that can be displayed on a view using something like "canvas.drawBitmap(bitmap, 0, 0, paint);".

I have spent many hours trying to access the Bitmap and I might have done it already, but even if I get the byte[] to return something it still wont render as a Bitmap on the canvas.

Could someone please help me here, I must be missing something. Thank you so much.

I believe it is supposed to accessed via...

PDFPage.java .... public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)

-or-

through Page.java -or- DocumentView.java -or- DecodeService.java

Like I said I have tried all of these and have gotten results I just cannot see where I am going wrong since I cannot render it to see if the Bitmap was called correctly.

Thank you again :)

CommonKnowledge
  • 769
  • 1
  • 10
  • 35

3 Answers3

2

The doc says the method returns "null if the image could not be decode." You can try:

byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);

I think This will help you:-

Render a byte[] as Bitmap in Android

How does Bitmap.Save(Stream, ImageFormat) format the data?

Copy image with alpha channel to clipboard with custom background color?

Community
  • 1
  • 1
Javascript Coder
  • 5,691
  • 8
  • 52
  • 98
1

if you want to get each pdf page as independent bitmap you should consider that VuDroid render the pages, PDFView only display them. you should use VuDroid functions.

now you can use this example and create your own codes

Example code : for make bitmap from a specific PDF page

    view = (ImageView)findViewById(R.id.imageView1);
    pdf_conext = new PdfContext();
    PdfDocument d = pdf_conext.openDocument(Environment.getExternalStorageDirectory()  + "your PDF path");

    PdfPage vuPage = d.getPage(1); // choose your page number
    RectF rf = new RectF();
    rf.bottom = rf.right = (float)1.0;
    Bitmap bitmap = vuPage.renderBitmap(60, 60, rf); //define width and height of bitmap

    view.setImageBitmap(bitmap);

for writing this bitmap on SDCARD :

try {   
    File mediaImage = new File(Environment.getExternalStorageDirectory().toString() + "your path for save thumbnail images ");
    FileOutputStream out = new FileOutputStream(mediaImage);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

for retrieve saved image:

        File file = new File(Environment.getExternalStorageDirectory().toString()+ "your path for save thumbnail images ");

        String path = file.getAbsolutePath(); 
        if (path != null){
            view = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), YOUR_X, YOUR_Y, false);
        }
ali5238
  • 11
  • 1
0
Try this code to check whether bitmap is properly generating or not

PdfContext pdf_conext = new PdfContext();
PdfDocument d = (PdfDocument) pdf_conext.openDocument(pdfPath);  

PdfPage vuPage = (PdfPage) d.getPage(0);  
RectF rf = new RectF();  

Bitmap bitmap = vuPage.renderBitmap(1000,600, rf);  

File dir1 = new File (root.getAbsolutePath() + "/IMAGES");
dir1.mkdirs();
String fname = "Image-"+ 2 +".jpg";
File file = new File (dir1, fname);
if (file.exists ()) 
file.delete (); 
try {
     FileOutputStream out = new FileOutputStream(file);
     bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
     out.flush();
     out.close();

   } catch (Exception e) {

 e.printStackTrace();

}
Raghavendra
  • 2,305
  • 25
  • 30