Merging Pictures in sequence - java

I am trying to make some kind of map maker, using the old 2D style of games such as Final Fantasy 4. Basically they had everything set up in a grid where each square on the grid might have taken 16x16 or 32x32 pixels.
I would like to start out small, and get the main things down first. Such as generating a map which could be, say, 128x128. This means, that I should be able to feed the program an array of numbers representing the different tiles available, and then the program should make a new picture by placing the tiles as the array specifies (So the one in Index 0 will be placed at 0,0 etc).
I plan to show the picture when I am done, but that should be easy as pie.
I've been looking around for a solution and all I could find was merging pictures on top of each other (as in layers on top of each other), rather than side by side, so can any one point me in the right direction? I'd like it if I didn't have to rely on 3rd party libraries, as this is more of a learning experience than practical application :)

First, create the output BufferedImage to be the size you need.
BufferedImage image = new BufferedImage(width, height, imageType);
Then, get the Graphics2D object from the image and start drawing the smaller image in the places they need to be in the resulting image:
Graphics2D g2 = image.createGraphics();
for (BufferedImage img : images) {
g2.drawImage(img, x, y, null);
}
Then, you can save the image to the desired format: jpg, png or gif.
ImageIO.write(image, "jpg", file);

Related

Graphics2D Interpolation not playing well with very small BufferedImages

I'm making a small game in Java, and it uses a pixel-graphics style, with many sprites 16x16 or 32x32 pixels. However, when I rotate them, I do not want "jaggies" along the side, so I used the RenderingHint
RenderingHint.KEY_INTERPOLATION
RenderingHint.VALUE_INTERPOLATION_BILINEAR
Unfortunately, because many of the images used are very small (16x16, 32x32) the resulting image is completely unusable. Output:
http://imgur.com/a/roRh4
As you can see, the small graphics are blurred. This is the intended effect for large images, but for small images, it is very bad.
One solution is to increase the resolution of all my graphics, while keeping the "blocky" effect. That would be a pain, so is there another way to tweak the interpolation?
Thank you guys so much.
Upscale your tiny images and use that instead:
BufferedImage tiny = ImageIO.read(new File("..."));
BufferedImage bigger = new BufferedImage(tiny.getWidth()*10, tiny.getHeight()*10, tiny.getType());
Graphics2D g = bigger.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.drawImage(tiny, 0, 0, bigger.getWidth(), bigger.getHeight(), 0, 0, tiny.getWidth(), tiny.getHeight(), null);
Replace the 10 scaling factor by the smallest value that gives acceptable results.
Do the rest of your rendering with high quality interpolation.

Java swing graphics image related questions

I am trying to write some programs where I load images to a panel.
Here are my 5 questions:
1> Is there any restriction on what kind(extension) of images can be loaded? I tried to load a .bmp file, it didn't load even after I renamed it with .jpg. However, some other file that were with extensions such as .png or .jpg loaded.
2>Is there a way I can cut an image through java to create a new image. Say, I have a 600x600 pixel image and I want to create a new image by selecting a 200x200 pixel from the middle of the original picture.
3>Is there a way I can resize an image?
4>Can I add an image to a scrollpane?
5> Can I rotate an image by an angle ,say 30 degree?
That's it. A little elaboration with examples will be nice. Thanks in advance.
1> Is there any restriction on what kind(extension) of images can be
loaded? I tried to load a .bmp file, it didn't load even after I
renamed it with .jpg. However, some other file that were with
extensions such as .png or .jpg loaded.
Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP. Image I/O is also extensible so that developers or administrators can "plug-in" support for additional formats. For example, plug-ins for TIFF and JPEG 2000 are separately available.
Check the Reading/Loading an image tutorial page
2>Is there a way I can cut an image through java to create a new
image. Say, I have a 600x600 pixel image and I want to create a new
image by selecting a 200x200 pixel from the middle of the original
picture.
Two ways. Croping the image using Clipping with Graphics. But faster approach is to use BufferedImage.getSubimage(int x, int y, int w, int h) method.
BufferedImage image = ImageIO.read("image file");
image = image.getSubimage(50, 50, 200, 200);
This will crop an image at location(x, y) == (50, 50) and size 200 x 200.
3>Is there a way I can resize an image?
The discussion about the various approach will take a size of a blog. Read through the The Perils of Image.getScaledInstance() article for good insight.
However, a quick approach for example: with cWidth and cHeight
BufferedImage tmpImage = new BufferedImage(cWidth, cHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)tmpImage.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(image, 0, 0, cWidth, cHeight, null);
There is working example done by MadProgrammer. It is also better to use external library such as this which does this works nicely.
4>Can I add an image to a scrollpane?
Direct adding is not suggested. use JLabel instead. Or use a custom component and override the paintComponent(Graphics g) function and draw inside it. There are some working example with these two approach. Generally working with JLabel is easier.
See this question answers. Mine including #MadProgrammer. And The custom painting official tutorial page.
5> Can I rotate an image by an angle ,say 30 degree?
Yes using the Graphics2D.rotate(double theta) function; There are actually much more things you can do with the Graphics2D API. Working examples are shown by MadProgrammer here and here.

Get an Area containing the non-transparent pixels in a BufferedImage in Java

In Java SE 7, I create a BufferedImage object:
BufferedImage i = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = i.createGraphics();
And then I fill parts of the image with color, using several call to fillPolygon, drawImage, etc. (Note that some of my colors have an alpha less than 255.)
My question is, how can I construct an Area object which contains only the area which was painted to in the BufferedImage? From reading the API docs, I believe it could be done by inspecting the Raster returned by the getAlphaRaster method, but I'm hoping there's an easier (and faster?) way of doing this.
..how can I construct an Area object which contains only the area which was painted to in the BufferedImage?
The getOutline(..) method seen in this source could be used to distinguish between the opaque and translucent parts of the image. Having said that, the method only checks for an 'exact match' between target color and image color for that pixel. You'd need to adjust it to instead check for alpha <255.
The ShapeContainment class would be used to determine if a point falls into any of a number of Area instances.
Other code deailing with Java-2D and images can be seen in OneRing & ShapeCollision.

Is it possible to add transparency to jpg in Java and Android

I want to load some images to the cloud, but I want to add some protections when someone views the photos and saves the images; they will not see anything because of the transparency.
would the code be common for Java and Android? I would like to prototype it in Java first.
I have found some code that combines two files. One file is my main file the other is a transparent file. The combined file does not have a transparent overlay.
Do I need to use an image drawing order?
http://www.developer.nokia.com/document/Java_Developers_Library_v2/GUID-D3E35E6F-0C45-48ED-B09D-F716E14C1C02/javax/microedition/amms/control/imageeffect/OverlayControl.html
BufferedImage image = ImageIO.read(new File("rose.jpg"));
BufferedImage overlay = ImageIO.read(new File("myimg1.gif"));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
ImageIO.write(combined, "PNG", new File("combined.png"));
This won't work. If they can see the image, they can copy it, one way or another. A solution to consider is providing watermarked thumbnails at no charge, then only offering the full resolution image for a fee. However, they can still copy the full resolution image once they pay.
You can actually put a trans image as an overlay to the orginial image, that will
Protect from download, I think this is usually done by the server side aka your cloud
I know from some websites that they use some kind of an overlay such as yours
And the browser can't see the image below so you can't download.
I actually didn't understand how you implementing this - the image is opened in the browser?
Just a wild though, you can also cut the image into pieces like a jigsaw puzzle
The device won't have problems connecting it togther but when you download you'll
Download only "one piece of the puzzle" :-P

Image manipulation using java

i'm trying to create a program that generates images for use as multi-screen backgrounds, i'm doing this targeted at windows (in my case, 7 so that basically i can get images to change without seeing the same image on two different screens)
in my program, i read multiple image input files and compile them into a single output image that is the total size of the desktop (including black areas not seen on screens)
my question is, what class/methods are good for cropping/resizing/pasting into a new image in java because i'm coming across so many image manipulation classes and they all seem to do one tiny thing.
i will not be modifying any of the images beyond resize or crop and putting it into a certain position in the new (initially blank) image.
code can be made available as i plan to release it at some later point for whoever may like/need it.
thank you in advance, if this question has been answered, my apologies but i DID have a look around.
I do not know if this is the best method, but it is quite easy:
// load an image
Image image = javax.imageio.ImageIO.read(new File("someimage.png");
// resize it
image = image.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
// create a new image to render to
BufferedImage newimg = new BufferedImage(200,100,BufferedImage.TYPE_INT_ARGB);
// get graphics to draw..
Graphics2D graphics =newimg.createGraphics();
//draw the other image on it
graphics.drawImage(image,0,0,null);
graphics.drawImage(image,100,0,null);
graphics.fillOval(20,20,40,40); //making it a bit ugly ;)
//export the new image
ImageIO.write(newimg,"png",new File("output.png"));
//done!
For simplicity I dropped all checks, exception handling, etc.

Categories

Resources