Dynamic Image Making - java

I want to make around 1000 images all are with just a few differences, I have a fixed size background color and logo placed. The difference is that on each one i want numbers from 1 to 1000 on one side and on the other side I have its representation on abacus, which can me made by hiding/showing 15 layers (Each containing 1 circle). So I am searching for a way to achieve this either by making a java program makes and saves all of those in 1000 .png files. Or maybe some other language than java. Note i just want someone to tell me how to put the numbers i will do the second part myself as you may not understand that. Mainly question is that I want 1000 png image files made automatically each with numbers 1 to 1000.

Use this, where x and y are the coordinates where you want to paint the numbers:
for(int i = 1; i < 1001; i++)
{
BufferedImage bi = ImageIO.read(new File("pathtoyourimage"));
Graphics2D g = bi.createGraphics();
g.drawString(i, x, y);
ImageIO.write(bi, "png", new File("outputpath/image" + i));
}

Related

Image Interpolation Help Needed

I'm trying to figure out how to use my thermal sensor to change colors to an overlay that I have over the Android camera. The problem is that the data I get back is in a 16x4 array. How do I resize this 16x4 grid to a different resolution? Such as 32x8, 48x12...etc.
Edit:
For instance, I have this as my draw method:
public void onDraw(Canvas canvas){
super.onDraw(canvas);
for(int x = 0; x < 4; x++){
for(int y = 0; y < 16; y++){
// mapping 2D array to 1D array
int index = x*GRID_WIDTH + y;
tempVal = currentTemperatureValues.get(index);
// 68x68 bitmap squares to display temperature data
if(tempVal >= 40.0)
bitmaps[x][y].eraseColor(Color.RED);
else if(tempVal < 40.0 && tempVal > 35.0)
bitmaps[x][y].eraseColor(Color.YELLOW);
else
bitmaps[x][y].eraseColor(Color.BLUE);
}
}
combinedBitmap = mergeBitmaps();
// combinedBitmap = fastblur(combinedBitmap, 45);
paint.setAlpha(alphaValue);
canvas.drawBitmap(combinedBitmap, xBitmap, yBitmap, paint);
Log.i(TAG,"Done drawing");
}
The current implementation is to draw to a 16x4 overlay over my camera preview, but resolution is very low, and I'd like to improve it the best I can.
The Bitmap class in the Android API (that's what I'm assuming you're using) has a static method called createScaledBitmap: http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29
What this method does is that it accepts an already created Bitmap, you specify the final width and height dimensions as well as a boolean flag called filter. Setting this to false does nearest neighbour interpolation while true does bilinear interpolation.
As an example, given that you have a 2D array of Bitmaps, you could resize one like so:
Bitmap resize = Bitmap.createScaledBitmap(bitmaps[x][y], 32, 8, true);
The first parameter is the Bitmap you want resized, the second parameter is the width, third parameter the height, and the last is the filter flag. The output (of course) is stored in resize and is your resized / scaled image. Currently, the Javadoc for this method (as you can see) provides no explanation for what filter does. I had to look at the Android source to figure out what exactly it was doing, and also from experience as I have used the method before.
Generally, you set this to false if you are shrinking the image, while you set this to true if you are upscaling the image. The reason why is because when you are interpolating an image from small to large, you are trying to create more information than what was initially available. Doing this with nearest neighbour will introduce blocking artifacts, and so bilinear interpolation will help smooth this out. Going from large to small has no noticeable artifacts using either method, and so you generally choose nearest neighbour as it's more computationally efficient. There will obviously be blurring as you resize to a larger image. The larger you go, the more blurriness you get, but that beats that blockiness you get with nearest neighbour.
For using just the Android API, this is the best and easiest solution you can get. If you want to get into more sophisticated interpolation techniques (Cubic, Lanczos, etc...), unfortunately you will have to implement that yourself. Try bilinear first and see what you get.

get percentage of image blur

Hi, so I have to make a script (doesn't matter what programming language, but i'll use Java here for example), a script that compares two black and white images and tells which one is blurred the most.
So I have to make a function like this:
function int getImageBlurPercentage()
{
ArrayList<Integer> ColorList = new ArrayList<Integer>();
//Part 1: fill ColorList with color values (0=black, 255=white)
go through Y axis
go through X axis
ColorList -> add colorValue of each pixel; [ie: 0 to 255]
//Part 2: process (This is the part where I need help !)
int lastColor = 0;
for(int color : ColorList)
{
// Something has to be done here
// To compare pixel by pixel
// and get noise result or difference result
// and convert it to a percentage (0% - 100%)
// This is where I need your help !
}
}
So this is where I need your help guys, I don't really know how to handle this.
I think this needs some math formulas which I suck at.
I would appreciate it if someone helps or gives a hint that could lead me to the right path. Thank you.
When you blur an image (let's say you use Gaussian blur), you actually doing some "averaging" on the pixels of the image, which means you make your edges "smoother".
So to check if one image has "smoother" edges then other, you can look on the Gradients of the image like Jan Dvorak suggested, but don't forget to normalize it by the amount of pixels in the image (otherwise larger images will get larger results).
If you want to check two entirely different images, the test will be much more complex, because different scenes naturally has different smoothness

Merging Pictures in sequence

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);

Java applet 2d graphics drawing issue when drawing twice (black parts)

So i had a console game i made, and i thought in making it 2d.
So far so good everything was fine.
I have movement and other things working fine.
And the drawing was fine,until i had this idea.
( my "map" is a Tile[][] )
I thought in creating a class that would represent a layer then a playable map would be a
Layer[] and i would draw the layers over each others and maybe store some on memory for faster drawing.
I thought this was a 'good'/'okay' idea.(i would appreciate if someone would tell me if it is a good idea or not)
The problem is when i use 2 Layers the second layer always messes the first one.
The code i use to draw the 'map' looks like this:
for (TileLayer layer : layers)
g.drawImage(layer.getImage(), 0, 0, null);
the layer.getImage() is correct (pretty sure, works with just one layer).
The problem is that when i draw 2 layers the second one blacks out everything in the first layer.
I have the layer draw code like this:
BufferedImage img = new BufferedImage(tiles.length * Tile.TILE_SIZE,
tiles[0].length*Tile.TILE_SIZE,
ColorSpace.TYPE_RGB);
for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < tiles[0].length; y++) {
//System.out.println("ID:" + tiles[x][y].getId());
img.getGraphics().drawImage(tiles[x][y].getImage(),
x * Tile.TILE_SIZE,
y * Tile.TILE_SIZE,
null);
}
}
Its a java applet i was overriding 'paint', i have changed that.
I tried using a transparent image to represent 'air' ( thought this should work probably messed it up).
tried adding a condition to prevent drawing when it shouldn't.
I might have made something that works in a wrong way.
Help on a proper way to do this would be nice.
(or tell me where I might have it wrong)
In the example, you appear to be using the wrong constant as the BufferedImage type.
If you want your BufferedImage to allow transparency, set the last parameter to BufferedImage.TYPE_INT_ARGB (or one of the type constants on BufferedImage that has "A" in the last bit).

Starting point for a tile map

I am due to start work on a 2D platform game in Java using Java2D, and am trying to devise a way to create a world. I have been reading up about this for the last few hours now, and as far as I can tell, a relatively effective way is to have a text file with a "matrix" of values in it, which is read in by the program in order to create the map (stored in a 2D array).
Now, my plan is to have multiple JComponents that display ImageIcons for the various textures in the world; the JComponent object would depend on the character in the given array index.
Is there anything I may have overlooked?
Will this schematic work with a background image, i.e. when there is a character that represents a blank space, will part of the background be shown?
Apologies if this seems like a lazy question, I can assure you it is not out of laziness. I am merely trying to plan this out before hacking code together.
Unless you have compelling reason to, having a different component for each tile is probably not a good way to go. Look into a Canvas and displaying loaded images at different offsets in it.
Example:
480x640 Canvas
128x16 image file(contains 8 16x16 tile images)
So your file has a bunch of numbers(characters etc.), we'll say 0-7 for the 8 tiles in the image. The file has 30x40 numbers, laid out in a grid the same as the canvas. So
1 2 1 3 4 8 2...
...
And to display the code ends up something like(not tested, based on docs)
Graphics g = //initialize graphics;
Image yourTileImage = //load your image;
for (int xpos = 0; xpos < maxX; xpos++)
for (int ypos = 0; ; ypos < maxY; ypos++)
int number = //get number from map file
g.drawImage(Image yourTileImage,
xpos * 16, ypos * 16, xpos * 16 + 15, ypos * 16 + 15,
number*16, 0, number+15, 15,
ImageObserver observer)
Which basically maps the number to your tile image, then puts that tile image into the right spot in the canvas(x,y) coordinates * size of tile.
There are a number of good 2d graphics engines available for Java. You would be better off using one of those rather than trying to re-invent the wheel. (Quite apart from anything else they will make use of the GPU.
You should easily find one that does what you need.

Categories

Resources