Jump to content

One last time, Java Help? :-)


Recommended Posts

Hey, I am here again stuck with a fairly simple problem but I cant really find a solution myself.

 

All I need to do is to rename the new image I am converting from.

(or name it before new image has been converted)

 

I will post the whole code but will highlight where I need help in.

 

I will describe what I am trying to do here.

So I am attempting to convert an image that has been selected to png. Now, the problem is that it's converting but is not making a new .png format. (e.g. I convert image from jpg to png, conversion works but does not paste the new image in the png format, the jpg is still there) I believe this error is because of the names and file explorer doesn't like having 2 of the same names so it does nothing. So I want the program to convert the image but with a different name. I've tried a few ways with no success. Maybe some help here? :)

 

/*
 * 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.*;
 
 
 
/**
 * 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");
        //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{
            File file;
            for (int i =0;i < files.size();i++) {
                file = files.get(i);
                BufferedImage image = ImageIO.read(file);
                File output = new File(current_dir.getAbsolutePath()+"\\"+file.getName());   
                output.createNewFile();
                if(!ImageIO.write(image, "png", output)){System.out.print("fail to write");}
                System.out.print(current_dir.getAbsolutePath()+"\\"+file.getName());
            }
        }
        catch(Exception e){
            System.out.print("fail");
            return false;
        }
        return true;
    }
}

 

 

That's all! Thanks!

 

- R. Berezon

Link to comment
Share on other sites

I am not 100% sure if I understand the problem, but I think the code is writing your converted image as filename.jpg and you want it to be filename.png. If that's the case, you could just do a string replace of .jpg to .png on the output filename, something like:

File output = new File( current_dir.getAbsolutePath( ) + "\\" + file.getName( ).replace( ".jpg", ".png" ) ); 

If your input images are in different formats other than jpg you would need a little more complex code to remove the filename extension and replace it for .png, you could remove the last 3 chars but .jpeg has 4 so that wont be a good solution. Another solution could be to just leave the original filename as is, and add ".png" at the end, so it wont matter what extension the original image has. For example, if you convert img005.jpg, you save it as img005.jpg.png. If you prefer to do that instead of replaceing .jpg for .png, use:

File output = new File( current_dir.getAbsolutePath( ) + "\\" + file.getName( ).concat( ".png" ) );  
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...