Need help with saving an image
With this programming assignment, I'm suppose to upload an image and divide into five pieces, and then save the image. Specifically, I'm having trouble saving an image after dividing the images. I want to save the new split images in the same location as I upload the image.
So lets say, i upload from C:/shark.jpg, I want to save the new split images in the same directory, C:/.
Under the private void saveImage() method, I know the statement, String myfilename = myfilename("") is wrong.
Code:
import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
// -------------------------------------------------------------------------
/**
* @author
* @version
*/
public class ImageBanner5 extends StudentTestableGraphicsProgram
{
//~ Instance/static variables .............................................
public static final int APPLICATION_WIDTH = 1024;
public static final int APPLICATION_HEIGHT = 600;
//declares buttons
GRect browseButton;
GRect clockwise;
GRect counter;
GRect greyscalebutton;
GRect bannerbutton;
GRect resetbutton;
GRect savebutton;
GLabel Browse;
GLabel Reset;
GLabel rotate1;
GLabel Banner;
GLabel Save;
GLabel counterclock1;
GLabel counterclock2;
GLabel greyscale;
GLabel Message;
// BufferedImage imageFile;
public double screenWidth;
public double screenHeight;
public GImage userpic;
public GImage result;
public String saveFile;
public GImage[] savesplitGimages = null;
;
//~ Methods ...............................................................
// ----------------------------------------------------------
/**
*
*/
public void init()
{
String file = chooseImage();
saveFile = file;
addMouseListeners();
// buttons (Browse, greyscale, Banner, counterclock, clockwise)
browseButton = new GRect(350, 120, 100, 50);
resetbutton = new GRect(350, 120, 100, 50);
clockwise = new GRect(350, 120, 100, 50);
counter = new GRect(350, 120, 100, 50);
greyscalebutton = new GRect(350, 120, 100, 50);
bannerbutton = new GRect(350, 120, 100, 50);
savebutton = new GRect(350, 120, 100, 50);
Message = new GLabel("");
Save = new GLabel("Save");
Save.setFont(new Font("Serif", Font.BOLD, 16));
Browse = new GLabel("Browse");
Browse.setFont(new Font("Serif", Font.BOLD, 16));
Banner = new GLabel("Banner");
Banner.setFont(new Font("Serif", Font.BOLD, 16));
Reset = new GLabel("Reset");
Reset.setFont(new Font("Serif", Font.BOLD, 16));
rotate1 = new GLabel("Rotate Clockwise");
rotate1.setFont(new Font("Serif", Font.BOLD, 13));
counterclock1 = new GLabel("Rotate");
counterclock2 = new GLabel("Counter-Clockwise");
counterclock1.setFont(new Font("Serif", Font.BOLD, 13));
counterclock2.setFont(new Font("Serif", Font.BOLD, 12));
greyscale = new GLabel("Greyscale");
greyscale.setFont(new Font("Serif", Font.BOLD, 14));
browseButton.setFillColor(Color.red);
browseButton.setFilled(true);
bannerbutton.setFillColor(Color.LIGHT_GRAY);
bannerbutton.setFilled(true);
savebutton.setFillColor(Color.PINK);
savebutton.setFilled(true);
resetbutton.setFillColor(Color.ORANGE);
resetbutton.setFilled(true);
clockwise.setFillColor(Color.green);
clockwise.setFilled(true);
counter.setFillColor(Color.blue);
counter.setFilled(true);
greyscalebutton.setFillColor(Color.yellow);
greyscalebutton.setFilled(true);
add(browseButton, 830, 50);
add(clockwise, 830, 130);
add(Browse, 855, 77);
add(rotate1, 835, 157);
add(counter, 830, 210);
add(counterclock1, 860, 225);
add(counterclock2, 830, 235);
add(greyscalebutton, 830, 290);
add(greyscale, 855, 325);
add(bannerbutton, 830, 370);
add(Banner, 855, 395);
add(resetbutton, 830, 450);
add(Reset, 855, 485);
add(savebutton, 830, 530);
add(Save, 855, 555);
savebutton.setVisible(false);
Save.setVisible(false);
showImage(file);
}
/**
*
*/
public void run()
{
}
public GImage resizeImage(GImage image)
{
//image automatically resizes to the width, 485
double sHeight = screenHeight;
double sWidth = screenWidth;
if(screenWidth > 485)
{
sWidth = 485.0;
}
else if(screenHeight > 485)
{
sHeight = 485;
}
if(sWidth < sHeight)
{
double scale =(sHeight) / sWidth;
double newHeight = sHeight / scale;
image = lossyResize(image, sWidth, newHeight);
}
else if(sWidth > sHeight)
{
double scale =(sWidth) / sHeight;
double newWidth = sWidth / scale;
image = lossyResize(image, newWidth, sHeight);
}
else
{
image = lossyResize(image, sWidth, sHeight);
}
return image;
}
/**
*
*/
public void mouseClicked(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
int[][] imgArray = userpic.getPixelArray();
int height = imgArray.length;
int width = imgArray[0].length;
//whenever the user clicks the following button, a certain action will occur
if(browseButton.contains(x, y))
{
displayNewImage();
}
else if(clockwise.contains(x, y))
{
rotateClockwise(imgArray, height, width);
}
else if(counter.contains(x, y))
{
rotateCounterclockwise(imgArray, height, width);
}
else if(greyscale.contains(x, y))
{
convertGreyScale(imgArray, height, width);
}
else if(bannerbutton.contains(x, y))
{
splitImages(imgArray, height, width);
}
else if(resetbutton.contains(x, y))
{
reset();
}
else if(savebutton.contains(x, y))
{
saveImage();
}
}
private void saveImage()
{
if(savesplitGimages != null && savesplitGimages.length > 0)
{
String myfilename = myfilename("");
for(int i = 0; i < savesplitGimages.length; i++)
{
GImage g = savesplitGimages[i];
if(g != null)
{
saveImage(g, myfilename + "_"+i + ".png");
// remove from the array
g = null;
}
}
Message.setLabel("Images Saved");
Message.setFont(new Font("Serif", Font.BOLD, 14));
add(Message, 50, 130);
}
savebutton.setVisible(false);
Save.setVisible(false);
}
private void reset()
{
if(saveFile != null && saveFile.length() > 0)
{
showImage(saveFile);
}
}
public void clearImeages()
{
if(userpic != null)
remove(userpic);
if(savesplitGimages != null && savesplitGimages.length > 0)
{
for(int x = 0; x < savesplitGimages.length; x++)
{
GImage g = savesplitGimages[x];
if(g != null)
{
remove(g);
g = null;
}
}
}
savebutton.setVisible(false);
Save.setVisible(false);
Message.setLabel("");
}
public void showImage(String file)
{
if(file != null && file.length() > 0)
{
clearImeages();
userpic = new GImage(file);
screenWidth = userpic.getWidth();
screenHeight = userpic.getHeight();
userpic = resizeImage(userpic);
add(userpic, 100, 100);
savebutton.setVisible(false);
Save.setVisible(false);
Message.setLabel("");
}
}
private void displayNewImage()
{
String fileName = chooseImage();
//save the filename
saveFile = fileName;
showImage(fileName);
}
private void splitImages(int[][] imgArray, int height, int width)
{
//Each banner part should be 97 pixels wide and 68 pixels high
remove(userpic);
GImage splitImage = null;
int maxwidth = 97;
int maxheight = 68;
int loop =(int) Math.floor(Double.valueOf(width / 97));
int[][] newArray = null;
savesplitGimages = new GImage[loop + 1];
for(int z = 0 ; z < loop ; z++)
{
newArray = new int[maxheight][maxwidth];
// calculate the last image end point for height and width
int widthcount =(maxwidth -1) *(z);
int heightcount =(maxheight -1) *(z);
for(int i = 0; i < maxheight -1; i++)
{
for(int j = 0; j < maxwidth -1; j++)
{
newArray[i][j] = imgArray[heightcount + i][widthcount + j];
}
}
splitImage = new GImage(newArray);
savesplitGimages[z] = splitImage;
add(splitImage, 250, 130 *(z));
}
//250, 130
savebutton.setVisible(true);
Save.setVisible(true);
}
private void rotateClockwise(int[][] imgArray, int height, int width)
{
int j = 0;
int i = 0;
//image rotates clockwise
int[][] newimgArray = new int[width][height];
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
newimgArray[j][height - 1 - i] = imgArray[i][j];
}
}
clearImeages();
userpic = new GImage(newimgArray);
add(userpic, 100, 100);
}
private void rotateCounterclockwise(int[][] imgArray, int height, int width)
{
//image rotates counterclockwise
int j = 0;
int i = 0;
int[][] newimgArray = new int[width][height];
for(i = 0; i < height; i++)
{
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
newimgArray[i][j] = imgArray[j][width - 1 - i];
}
}
}
clearImeages();
userpic = new GImage(newimgArray);
add(userpic, 100, 100);
}
public void convertGreyScale(int[][] imgArray, int height, int width)
{
//image is converted to greyscale
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
int pixel = imgArray[i][j];
int r = GImage.getRed(pixel);
int g = GImage.getGreen(pixel);
int b = GImage.getBlue(pixel);
int xx = computeGreyScaleLuminosity(r, g, b);
imgArray[i][j] = GImage.createRGBPixel(xx, xx, xx);
}
}
clearImeages();
userpic = new GImage(imgArray);
add(userpic, 100, 100);
}
private int computeGreyScaleLuminosity(int r, int g, int b)
{
Long l = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
return l.intValue();
}
public GImage cropTo(GImage orig, int width, int height)
{
int[][] imgArray = orig.getPixelArray();
int origHeight = imgArray.length;
int origWidth = imgArray[0].length;
int[][] newArray = new int[width][height];
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
newArray[i][j] = imgArray[i][j];
}
}
GImage result = new GImage(newArray);
return result;
}
/**
* [Author: Kevin Buffardi] Converts a GImage to be of a new width and height and returns it
* Note that this process is "lossy" so the returned GImage will have lesser resolution than
* the original if it is smaller size, and there is not a way to reverse this loss of precision
* @param orig, the original GImage to resize
* @param width the desired new width of the image
* @param height the desired new height of the image
* @return the GImage orig, resized to widthXheight
*/
public GImage lossyResize(GImage orig, double width, double height)
{
return new GImage(orig.getImage().getScaledInstance((int) width,(int) height, 0));
}
/**
* [Author: Kevin] Saves a GImage to disk as a png file
* @param picture A GImage picture that will be converted to png format and saved on disk
* @param filename The name of the file to save (path optional, default to project folder. e.g. "output.png")
*/
public void saveImage(GImage picture, String filename)
{
try
{
File name = new File(filename);
Image img = picture.getImage();
BufferedImage buff = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics graph = buff.getGraphics();
graph.drawImage(img, 0, 0, null);
graph.dispose();
ImageIO.write(buff, "png",name);
}
catch(IOException e)
{
System.out.println("Error when writing file: " + e.toString());
}
}
/**
* [Author: Kevin] Shows a file browser dialog so the user can choose a file.
* Only image files *should* be chosen, but this method does not prevent other types of files to be chosen.
* @return A string representing the path and filename chosen (e.g. "C:\My Documents\image.jpg")
*/
public String chooseImage()
{
JFileChooser dialog = new JFileChooser();
int result = dialog.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION)
{
return dialog.getSelectedFile().getPath();
}
else
return "";
}
public static void main(String[] args)
{
new ImageBanner5().start();
}
}
Let me know if you guys need any more information.