Results 1 to 3 of 3
- 04-30-2012, 04:06 PM #1
Member
- Join Date
- Dec 2011
- Location
- Belgium
- Posts
- 25
- Rep Power
- 0
Cut Java image into different pieces with an ImageBuffer
Hey,
I want to cut an image in java into several pieces with an ImageBuffer. When I try to do this with this code it cut's the image in 32 pieces (wich I want) but it doesn't change position and gives me 32-times te same image.
I want 32 different pictograms from one imagefile (size of imagefile 800 x 400).
This is what I made of it? (The java applet was just for testing)
Java Code:public class GetImage { private String filename; public GetImage() { } public GetImage(String filename) { this.filename = filename; } public BufferedImage getImageNow() throws IOException { File file = new File(this.filename); FileInputStream fis = new FileInputStream(file); BufferedImage image = ImageIO.read(fis); return image; } }
Java Code:public class SplitImage { private int rows; private int columns; private int chuncks; private int chunckheigth; private int chunckwidth; private BufferedImage image; private BufferedImage images[]; public SplitImage() { } public SplitImage(int rows, int columns, BufferedImage image) { this.row = rows; this.columns = columns; this.chuncks = this.rows * this.columns; this.image = image; this.chunckwith = 102; this.chunckheitgh = 97; } public BufferedImage[] splitImageNow() throws IOException{ int count = 0; images = new BufferedImage[chuncks]; for (int x = 0; x < rows; x++) { for (int y = 0; y < columns; y++) { //Initialize the image array with image chunks images[count] = image.getSubimage(0, 0, this.chunckwidth, this.chunckheigth); Graphics2D gr = images[count++].createGraphics(); //gr.drawImage(image, 0, 0,chunckwidth,chunckheigth,null); gr.drawImage(image, 0, 0, chunckwidth, chunckheigth, chunckwidth * y, chunckheigth * x, chunckwidth * y + chunckwidth, chunckheigth * x + chunckwidth, null); gr.dispose(); } } return images; } public void saveImages (BufferedImage[] images) throws IOException { for (int i = 0; i < images.length; i++) { ImageIO.write(images[i], "png", new File("img" + i + ".png")); } } }
Java Code:public class Application_GUI extends Applet { GetImage getImage; SplitImage splitImage; public void init() { setSize(500,500); try { getImage = new GetImage("Pictograms.png"); splitImage = new SplitImage(4,8,getImage.getImageNow()); splitImage.saveImages(splitImage.splitImageNow()); } catch (Exception e) { System.out.println("something has gone wrong"); } } }
- 04-30-2012, 04:38 PM #2
Re: Cut Java image into different pieces with an ImageBuffer
Moved from New to Java
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-30-2012, 04:46 PM #3
Re: Cut Java image into different pieces with an ImageBuffer
1. Read the API for getSubImage(...) and you'll find that it shares the same data array as the original image. Any drawing you do to on the subimage is going to affect the original image.
2. Read the API for the overload of Graphics#drawImage that you use (the one that takes 8 int arguments). The significance of each of those arguments is clearly defined.
But, if you're dealing with an image that doesn't change, why not just use subimages obtained with relevant coordinates? There's really no point in getting a subimage from the top-left corner and then trying to draw something over it.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
How to import image, draw circles/text on it, and save a new image to disk
By InTheEndo in forum Java 2DReplies: 1Last Post: 07-28-2011, 09:48 AM -
Battleship Game - Pieces lay on top of eachother.
By lolerskates2695 in forum New To JavaReplies: 7Last Post: 04-25-2011, 02:05 AM -
Java Applet loading image; render as you get image?
By ea25 in forum New To JavaReplies: 12Last Post: 04-14-2011, 02:58 PM -
Triangle Areas - Please help before I smash my laptop into pieces!
By Atia of the julii in forum New To JavaReplies: 67Last Post: 11-10-2010, 06:40 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 06:29 PM
Bookmarks