Java Question Returning Bitmap to Bitmap.Initalize3 Problem

keirS

Well-Known Member
Licensed User
Longtime User
I am trying to wrap the PDFRenderer API for Lollipop. I have the following Java code:

B4X:
 public Bitmap RenderPage(int PageNumber) {
   
    PdfRenderer.Page page = renderer.openPage(PageNumber);
       
   
   
    BA.Log(Integer.toString(page.getWidth()));
    BA.Log(Integer.toString(page.getHeight()));
   
    Bitmap bmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_4444);

   
    page.render(bmap,null,null,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
    page.close();
    BA.Log(Integer.toString(bmap.getByteCount()));
   
     
    return bmap;
    }
   
    /**
     *  Renders Bitmap to PNG File
     *
     */
  public void RenderPageToFile(int PageNumber,String Path, String FileName) throws FileNotFoundException {
       
        PdfRenderer.Page page = renderer.openPage(PageNumber);
           
       
       
        BA.Log(Integer.toString(page.getWidth()));
        BA.Log(Integer.toString(page.getHeight()));
       
        Bitmap bmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_4444);

         File file = new java.io.File(Path,FileName);
        page.render(bmap,null,null,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
        page.close();
        bmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file))   ;
    }

my B4A test code:

B4X:
Sub Process_Globals
    Dim sWriteDir As String
    Dim PDFRenderer As PDFRender
     
End Sub

Sub Globals
    Dim showPDF As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    Dim b As Bitmap

    sWriteDir = File.DirRootExternal & "/PDFTest"
     PDFRenderer.Initialize(sWriteDir,"Test.pdf")
    b.Initialize3(PDFRenderer.RenderPage(0))
    b.WriteToStream(File.OpenOutput(sWriteDir,"TestFromBA.png",False),100,"PNG")
    PDFRenderer.RenderPageToFile(0,sWriteDir,"Test.png")
     
     
End Sub

RenderPageToFile works fine and generates exactly what I would expect. The initialize3 call and WriteToStream generates a zero byte file. The Log for ByteCount shows 2003960.
 

keirS

Well-Known Member
Licensed User
Longtime User
Turns out it's nothing to do with the wrapper it's my B4A code. Just assumed B4A's outputstream would flush like it does in RenderpageToFile in the Java code. New test code works.

B4X:
    Dim b1 As Bitmap
     Dim b2 As Bitmap
    Dim b3 As Bitmap
     
    Dim o1 As OutputStream
    Dim o2 As OutputStream
    Dim o3 As OutputStream
     sWriteDir = File.DirRootExternal & "/PDFTest"
    o1 = File.OpenOutput(sWriteDir,"TestFromb1.png",False)
    o2 = File.OpenOutput(sWriteDir,"TestFromB2.png",False)
    o3 = File.OpenOutput(sWriteDir,"TestFromB3.png",False)
     
   
     PDFRenderer.Initialize(sWriteDir,"Test.pdf")
    b1.Initialize3(PDFRenderer.RenderPage(0))
    b1.WriteToStream(o1,100,"PNG")
    PDFRenderer.RenderPageToFile(0,sWriteDir,"Test.png")
    b2.Initialize(sWriteDir,"Test.png")
    b2.WriteToStream(o2,100,"PNG")
    b3.Initialize3(b2)
    b3.WriteToStream(o3,100,"PNG")
    o1.Close
    o2.Close
    o3.Close
 
Top