Jump to content

More Java help?


Recommended Posts

Hey there ahoyworld community! I am back again with another problem in java and am seeking help.
 
I am trying to get the image information to convert them to .png image format.
 
Here is the code and I will bold the section that I need help in.
 
Thanks in advance!
 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.File;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.IOException;
/**
 * this class exposes function that tell the main program what this creates
 * and actually creates the new file at the specified location 
 * @author Ryan Berezon
 */
public class Plug implements File_plug_in
{
    //returns a string representing the type of operation this class preforms 
    @Override
    public String get_type()
    {
        return "CompressFile";// creates new file 
    }
    
    // returns the button to be displayed for this plug in 
    @Override
    public JButton get_Button(String IconDir)
    {
        //Name of button
        JButton jb = new JButton("Convert 2 PNG");
        //Size of button
        jb.setPreferredSize(new java.awt.Dimension(100,50));
        //Image for the button
        jb.setIcon(new Icon_imp(IconDir +"image_file.png"));
        return jb;
    } 
 
    @Override
    public boolean operate_on_file(java.util.ArrayList<java.io.File> files,File current_dir) 
    {
        try{
            // Create a directory; all non-existent ancestor directories are automatically created
            
            File input = (new File(current_dir+"/Converted_Image.jpg"));
            ImageIO.createImageInputStream(files);
            
            File output = (new File(current_dir+"/PNG_Converted_Image.png"));            
            ImageIO.write(ImageIO.read(input), "png", output);
            return true;
            
        }
        catch(Exception e){}//fail
        return false;
        
                
    }
    
}

 
If you can help me and maybe give me a few pointers so that I may not get stuck with this again!
 
Thanks!
 
R. Berezon

Edited by Rarek
Put in a [CODE] block. :D
Link to comment
Share on other sites

I mostly work in C# but here's my attempt working with what you posted.

 

public boolean operate_on_file(java.util.ArrayList<java.io.File> files,File current_dir)   {
        try{
            for (File file : files) {
                BufferedImage image = ImageIO.read(file);
                File output = new File(current_dir+"/"+file.getName());            
                ImageIO.write(image, "png", output);
            }
        }
        catch(Exception e){
            return false;
        }
        return true;
    }
}
Edited by Rarek
Also put in a CODE block
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Forum Statistics

    11.1k
    Total Topics
    66.4k
    Total Posts
×
×
  • Create New...