Re: Save contents of image icon.
On Jul 11, 11:06 am, bH <bherbs...@hotmail.com> wrote:
Hi All,
I have a jpg image that has been cut up into 16 pieces. I need help to
save each of the pieces.
Any help is appreciated.
TIA
bH
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.*;
public class DiceImage extends JFrame {
BufferedImage bi = null;
DiceImage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
try {
bi = ImageIO.read(new File("images/ImageToDice.jpg"));
} catch(IOException ioe) {
ioe.printStackTrace();
throw new RuntimeException(ioe);
}
setLayout(new FlowLayout());
if(bi != null) {
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++) {
add(new JLabel(new
ImageIcon(bi.getSubimage(i*bi.getWidth()/4, j*bi.getHeight()/4,
bi.getWidth()/4,
bi.getHeight()/4))));
}
}
pack();
}
public static void main(String[] args) {
DiceImage diceImage = new DiceImage();
diceImage.init();
diceImage.setVisible(true);
}
}
Hi All,
Thanks to Roedy's mindprod.com
http://mindprod.com/jgloss/imageio.html#FROMFILE
demo from website :
Saving a BufferedImage to a file
// BufferedImage to File
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
....
ImageIO.write( aBufferedImage, "jpeg" /* "png" "jpeg" format desired,
no "gif" yet. */, new File ( "snap.jpg" ) /* target */ );
my program with revisions now saves 16 XXX.jpg images :
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class DiceImage extends JFrame {
BufferedImage bi = null;
int C = 0;
String PixName;
DiceImage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
try {
bi = ImageIO.read(new File("images/ImageToDice.jpg"));
} catch(IOException ioe) {
ioe.printStackTrace();
//throw new RuntimeException(ioe);
}
setLayout(new FlowLayout());
if(bi != null) {
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++) {
PixName = "Pix" + C;
System.out.println(PixName);// review name output
//screen output
add(new JLabel(new ImageIcon(bi.getSubimage(i*bi.getWidth()/
4,j*bi.getHeight()/4, bi.getWidth()/4,bi.getHeight()/4))));// image
file output
try {
ImageIO.write((bi.getSubimage(i*bi.getWidth()/4,
j*bi.getHeight()/4, bi.getWidth()/4,bi.getHeight()/4)), "jpeg", new
File("images/"+ PixName +
".jpg"));
C++;
}
catch ( IOException e )
{
System.out.println( "image missing" );
}
}
pack();
}
}
public static void main(String[] args) {
DiceImage diceImage = new DiceImage();
diceImage.init();
diceImage.setVisible(true);
}
}
bH