B4J Question Is there a problem with my java code?

liulifeng77

Active Member
Licensed User
Longtime User
I'm a java beginner,
I build a library to convert a powerpoint file to jpgs. It takes more than 100 seconds.
Convert the same file, VB.net program only took 30 seconds.
Is there a problem with my java code?

java code:
transfer:
package b4x.doc;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.documents.ImageType;
import com.spire.presentation.*;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
@ShortName("Doc")


public class doc {
  static BA b;
  static String source;
  static String dir;
  static String myevent;
  static String mytypename;
  static String pic;

    public static void transfer(BA ba,String event,String source_dir,String todir,String lei,String pic_name)  throws IOException{
      
        b=ba;
        source=source_dir;
        dir=todir;
        myevent=event;
        pic=pic_name;
        if(lei=="ppt"){
         ppt_trans ppt=new ppt_trans();
         Thread t2=new Thread(ppt);
         t2.start();
        }
        if(lei=="word"){
         doc_trans doc=new doc_trans();
         Thread t1 = new Thread(doc);
         t1.start();
        }
        
        
      
    }
    
    

public static class doc_trans implements Runnable{
  
    Document doc=new Document();
    public void run() {
      
      
        doc.loadFromFile(source);
        BufferedImage[] images=doc.saveToImages(0,doc.getPageCount(),ImageType.Bitmap,70,70);
         int i=0;
       for(BufferedImage image: images)
       {
      
        File file= new File(dir+"\\"+String.format((pic+"-%d.JPG"), i));
        try {
            ImageIO.write(image, "JPG", file);
        } catch (IOException e) {
          
            e.printStackTrace();
        }
        i++;
    }
    if (b.subExists(myevent+"_finished")){
        
        b.raiseEvent(b, myevent+"_finished","success");

       System.out.println("output success");
    
    }
        
    }
}
public static class ppt_trans implements Runnable{
    Presentation p= new Presentation();
    [USER=69643]@override[/USER]
    public void run() {
        try {
            p.loadFromFile(source);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < p.getSlides().getCount(); i++) {
       BufferedImage image = p.getSlides().get(i).saveAsImage();
      
        String fileName =  String.format(dir+"/"+pic+"-%1$s.JPG", i);
        try {
            ImageIO.write(image, "JPG",new File(fileName));
          
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    if (b.subExists(myevent+"_finished")){
        
        b.raiseEvent(b, myevent+"_finished","success");

       System.out.println("output success");
    
    }
    p.dispose();

    
}



}
}

vb.net code
vb.net:
Module Module1

    Sub Main()

        Dim LData As String ="XXXXXXXXX"
        Spire.License.LicenseProvider.SetLicenseKey(LData)
        Dim a As String = Microsoft.VisualBasic.Command
        Dim doc As Document = New Document
        Dim getpara() As String = a.Split(" ")

        If getpara.Length < 3 Then
            Console.Write("need three paras")
            Return
        End If
        Dim para = getpara.Length
        Dim custom_filename As String = ""
        Dim target_dir As String
        target_dir = getpara(2)
        Dim source_dir As String
        source_dir = getpara(1)
        custom_filename = getpara(0)


        If custom_filename = "" Then
            Console.Write("no paras")

            Return

        End If
        'System.IO.Directory.CreateDirectory(dir)


        doc.LoadFromFile(source_dir + custom_filename)
        Dim str As String = ""
        Dim images() As Image = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile)
        Dim i As Integer = 0
        Do While (i < images.Length)
            Dim outputfile As String = String.Format(custom_filename + "_" + "image-{0}.png", i)
            If str = "" Then
                str = str + outputfile
            Else

                str = str + "#" + outputfile
            End If
            images(i).Save(target_dir + outputfile, System.Drawing.Imaging.ImageFormat.Png)
            i = (i + 1)
        Loop
        System.Diagnostics.Debug.WriteLine(str)
        Console.Write(str)

    End Sub

End Module
 

agraham

Expert
Licensed User
Longtime User
I would expect the bulk of the time in both case to be taken up by the generation of the jpg data so I suspect that you are seeing the relative efficiencies of the JvaFX and native GDI+ image handling. In my experience GDI+, which System.Drawing.Imaging is a thin veneer on top of, is pretty well optimised being raw C/C++ code. You could try writing a simple multiple image save test to check this.
 
Upvote 0
Top