Solved

# Combining bmp's on the fly?

Posted on 2000-05-11
221 Views
Last Modified: 2010-04-01
Greetings,

I have the following problem:

I have two bmp's of equal dimensions. I would like to combine part of one (depending on an equation involving time) with the opposite part of the second bitmap to produce a third.

I would like to create a single bitmap for this process so I could proceed to use it as a texture for a 3d object.

This process would need to be repeated roughly every ten minutes.

Any ideas/code snippets would be greatly appreciated.

Thanks in advance.
0
Question by:setaglibb
• 5
• 3
• 2
10 Comments

LVL 7

Expert Comment

ID: 2802095
What do you mean by bitmap? Do you have 2 Image objects and you want to combine them to create another one? If so, here's how you do it:

public static Image combineImages(Image img1, Image img2){
Canvas c = new Canvas();
Image newImage = c.createImage(img1.getWidth(null)*2,img1.getHeight(null));
Graphics g = newImage.getGraphics();
g.drawImage(img1,0,0,null);
g.drawImage(img2,newImage.getWidth(null)/2,0,null);
return newImage;
}

This will make the new Image the combination of the 2 given images, one next to the other.
If you want to draw only a part of an image use the Graphics.clipRect(x,y,width,height) to set the clipping rectangle before you draw...

If you have any questions or wish to clarify what you want, please post a comment.

Hope this helps, Sasha.
0

Author Comment

ID: 2802152
To clarify:

I have two bitmaps (of equal dimensions) stored on my hard drive that I want to access during runtime and combine parts of them to produce a single image.

I would also like to save the combined image in the bmp format (if possible) on the fly.

The area to be replaced will be bounded by a sin wave so I don't think cliprect will help.

I am new to this image processing stuff so I'm not even sure how to access/store them in my program.

By what you have told me so far I assume they are stored internally as image objects?

Or do I have to use more advanced stuff like bufferedimage, bitmapinfoheaders, and copyoffscreenregion?

Hope this makes more sense now.
0

LVL 7

Expert Comment

ID: 2804083
>> Hope this makes more sense now.
Yeah, it does :-)

Ok, since the file is in bmp format, the easiest thing to do will be to open the file as regular binary file, read the contents into a matrix that will represent the image and perform all operations on that matrix. It would be very easy to copy portions of the matirx into another matrix, even if it involves complex functions... you just copy parts of an array by some rule. After you are done, again, if this was another format, it would have been more difficult, but since it's bmp, you can just write the file as binary on the hard disk...

Can you do that yourself or do you need examples in file I/O and copying matrices?
0

Author Comment

ID: 2804834
That's a big help but I would appreciate some file I/O and copying matrices examples if you don't mind.

Thanks for your time.
0

LVL 7

Expert Comment

ID: 2805171
Umm, ok, well, here's a method that reads an array of bytes of given length from a given file:

public static byte[] readArrayFromFile(String fileName, int length) throws IOException, FileNotFoundException{
InputStream input = new FileInputStream(fileName);
byte [] byteArray = new byte[length];
input.read(byteArray);
input.close();
return byteArray;
}

Here's a method that reads one byte from a given file:

public static byte readByteFromFile(String fileName) throws IOException, FileNotFoundException{
InputStream input = new FileInputStream(fileName);
byte readByte = (byte)input.read();
input.close();
return readByte;
}

I am not sure what exactly is the bmp format, but I suppose its 2 numbers specifying width and height of the image and then the all the pixel colors, row by row. You should find the exact specification somewhere on the web.

Well, copying matrices is pretty straightforward, this method will take 2 matrices of same dimensions and return a new one which will be the combination of the given ones in such a way that the upper left triangle half of the 1st matrix will be combined with the lower right triangle half of the 2nd matrix:

public static int[][] combineMatrices(int [][] matrix1, int [][] matrix2){
if ((matrix1.length!=matrix2.length)||(matrix1[0].length!=matrix2[0].length))
throw IllegalArgumentException;
int width = matrix1.length;
int height = matrix1[0].length;
int [][] newMatrix = new int[width][height];
for (int i=0;i<width;i++)
for (int j=0;j<height;j++){
if (j*width+i*height<=width*height)
newMatrix[i][j] = matrix1[i][j];
else
newMatrix[i][j] = matrix2[i][j];
}
return newMatrix;
}

You may also use the System.arraycopy method to imporove efficiency when copying arrays.

I didn't compile or test this code, so there may be some small bugs, but the idea is correct.

Good luck, Sasha :-)
0

LVL 7

Expert Comment

ID: 2810087
Umm, ok, so did my suggestion help you?
0

LVL 7

Accepted Solution

Sasha_Mapa earned 100 total points
ID: 2853387
Posting an answer based on the fact that I answered the question.
0

LVL 3

Expert Comment

ID: 2945771
Reviewing question.

darinw
Customer Service
0

LVL 3

Expert Comment

ID: 3090801
Answer accepted
0

LVL 3

Expert Comment

ID: 3090802
Moving question to the PAQ.

darinw
Customer Service

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be uâ€¦
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how â€¦
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arrâ€¦

#### 810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.