I try to join two images. But not able to create it.
I am not able to understand what is the problem in the below code.
It is not able to create the concat.jpg file.
BufferedImage image = ImageIO.read(mainFile);
BufferedImage image1 = ImageIO.read(fileToMerge);
int width = Math.max(image.getWidth() , image1.getWidth());
int height = Math.max(image.getHeight() , image1.getHeight());
log.info("width {}", width);
log.info("height {}", height);
BufferedImage concatImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = concatImage.createGraphics();
graphics2D.drawImage(image, 0, image.getHeight(), null);
graphics2D.drawImage(image1, 0 , image1.getHeight(), null);
ImageIO.write(concatImage, "jpg", new File(Constants.LOCAL_FOLDER + "/concat.jpg"));
You want the height of concatImage to be the combined height of image and image1.
You want to draw image at coordinates (0,0) and you want to draw image1 at x = 0 and y = height of image
Try the following.
BufferedImage image = ImageIO.read(mainFile);
BufferedImage image1 = ImageIO.read(fileToMerge);
int width = Math.max(image.getWidth() , image1.getWidth());
int height = image.getHeight() + image1.getHeight();
log.info("width {}", width);
log.info("height {}", height);
BufferedImage concatImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = concatImage.createGraphics();
graphics2D.drawImage(image, 0, 0, null);
graphics2D.drawImage(image1, 0, image.getHeight(), null);
ImageIO.write(concatImage, "jpg", new File(Constants.LOCAL_FOLDER + "/concat.jpg"));
Note that you should call graphics2D.dispose() when you no longer need to use it.
Related
I have a image of dimension 215*112. I want to make it 215*142.
Src Img:
I used the following code:
BufferedImage image = ImageIO.read(new File("src.png"));
int h = 15;
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight() + 2 * h, image.getType());
Graphics g = newImage.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, image.getWidth(), image.getHeight() + 2 * h);
g.drawImage(image, 0, h, null);
g.dispose();
ImageIO.write(newImage, "png", new File("dest.png"));
I am getting following result:
Why the padding is also getting added to x direction?
Because the source you are giving has two transparent vertical bars on the left and right of the image
Use this image
I need to draw a new image over old image. I first opened both images in BufferedImage and changed their white background to transparent. Then I got a Graphics2D object from the bufferedImage of old image and called drawImage method of Graphics2D class. I then saved the old image to disk. When I open the saved image I find only the old image with white background changed to transparent. Can anyone suggest me what is error with my code or how can I get to fix my error ?
BufferedImage newImage = ImageIO.read(new File("new.png"));
BufferedImage oldImage = ImageIO.read(new File("old.png"));
newImage = makeWhiteTransparent(newImage);
oldImage = makeWhiteTransparent(oldImage);
Graphics2D graphics = (Graphics2D) oldImage.getGraphics();
graphics.drawImage(newImage,null, 0,0);
File outputImage = new File("merged.png");
ImageIO.write(oldImage, "png", outputImage);
My makeWhiteTransparent method goes like this:
public static BufferedImage makeWhiteTransparent(BufferedImage img){
BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
dst.getGraphics().drawImage(img, 0, 0, null);
int markerRGB = Color.WHITE.getRGB() | 0xFF000000;
int width = dst.getWidth();
int height = dst.getHeight();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int rgb = dst.getRGB(x, y);
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
int value = 0x00FFFFFF & rgb;
dst.setRGB(x, y, value);
}
}
}
return dst;
}
I tried changing graphics.drawImage(newImage, null,0,0) to graphics.drawImage(newImage, 0,0, null) and also changing TYPE_4BYTE_ABGR to TYPE_INT_ARGB as suggested but it did nothing. The error still exists.
This needs to be changed:
graphics.drawImage(newImage,null, 0,0);
to
graphics.drawImage(newImage, 0,0, null);
you are using the wrong version of drawImage - check http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
--
Change also the type TYPE_4BYTE_ABGR to TYPE_INT_ARGB
--
Here's how it works for me:
public BufferedImage makeWhiteTransparent(BufferedImage img){
BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(img, 0, 0, null);
int markerRGB = 0x00ffffff; // Color.WHITE.getRGB() | 0xFF000000;
int width = dst.getWidth();
int height = dst.getHeight();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int rgb = dst.getRGB(x, y)&0x00ffffff;
if ( rgb == markerRGB ) {
int value = 0x00FFFFFF & rgb;
dst.setRGB(x, y, value);
}
}
}
return dst;
}
bim = makeWhiteTransparent(bim);
bim2 = makeWhiteTransparent(bim2);
Graphics2D graphics = (Graphics2D) bim.getGraphics();
graphics.drawImage(bim2,0,0, null);
g2.drawImage(bim, w/2-wc/2, h/2-hc/2, null);
I got the answer to my question finally. All I had to do was create a new BufferedImage and draw two images over it. Below is the code that works as expected:
BufferedImage newImage = ImageIO.read(new File("new.png"));
BufferedImage oldImage = ImageIO.read(new File("old.png"));
oldImage = makeWhiteTransparent(oldImage);
newImage = makeWhiteTransparent(newImage);
int width = Math.max(newImage.getWidth(), oldImage.getWidth());
int height = Math.max(newImage.getHeight(), oldImage.getHeight());
BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = combined.getGraphics();
graphics.drawImage(oldImage, 0, 0, null);
graphics.drawImage(newImage, 0, 0, null);
File outputImage = new File("merged.png");
ImageIO.write(combined, "PNG", outputImage);
I have this problem. I am using this code to rotate an image but the rotated image has black padding in its corners due to rotation.
How could I remove it?
public static BufferedImage rotate(BufferedImage img, int angle) {
rotate_checked = false;
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg =new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);
return dimg;
}
You need to create a transparent image:
BufferedImage buffer = gc.createCompatibleImage(height, width, Transparency.TRANSLUCENT);
where 'gc' is a Graphics2D object. You can also create one directly with new BufferedImage() of course, but this will give you the most efficient-to-use image for your particular graphics context.
i am working on image comparison in java. I think before going to compare the images, it is better to process the images for setting a fixed size image. Is there any java functionality to resize the images. I want to rescale the images to 300*225.
BufferedImage img = ImageIO.read(imageFile);
Image scaled = img.getScaledInstance(300, 255, Image.SCALE_DEFAULT);
You can also take a look at the java-image-scaling library.
public ImageIcon resizeImage(String filePath, int w, int h) {
String data = filePath;
BufferedImage bsrc, bdest;
ImageIcon theIcon;
//scale the image
try
{
bsrc = ImageIO.read(new File(data));
bdest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bdest.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance((double) w / bsrc.getWidth(),
(double) h / bsrc.getHeight());
g.drawRenderedImage(bsrc, at);
//add the scaled image
theIcon = new ImageIcon(bdest);
return theIcon;
}
catch (Exception e)
{
System.out.println("This image can not be resized. Please check the path and type of file.");
return null;
}
}
BufferedImage createResizedCopy(Image originalImage,
int scaledWidth, int scaledHeight,
boolean preserveAlpha)
{
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
Graphics2D g = scaledBI.createGraphics();
if (preserveAlpha) {
g.setComposite(AlphaComposite.Src);
}
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
return scaledBI;
}
This threads answers your question well
Very interesting article on manipulating images
Is there any faster way to achieve padding of pixels to a BufferedImage than drawing it centered on larger BufferedImage?
BufferedImage has a constructor where you get to specify a WriteableRaster.
Picking at the a default buffered image, storing each pixel in an int, it uses an IntegerInterleavedRaster.
The ColorModel you can use ColorModel.getRGBDefault().
int imageWidth = 638, imageHeight = 480;
int dataImageWidth = 640;
SampleModel sm = new SinglePixelPackedSampleModel(TYPE_INT, imageWidth, imageHeight, dataImageWidth, new int[] { 0xff0000, 0xff00, 0xff });
DataBuffer db = new DataBufferInt(dataImageWidth * imageHeight);
WritableRaster r = Raster.createWritableRaster(sm, db, new Point());
BufferedImage image = new BufferedImage(ColorModel.getRGBDefault(), r, false, null);
Notice the scanlineStride in SinglePixelPackedSampleModel (second last parameter).
Another much simpler approach is to use BufferedImage's getSubimage method.
BufferedImage fullImage = new BufferedImage(dataImageWidth, imageHeight);
BufferedImage subImage = fullImage.getSubimage(0, 0, imageWidth, imageHeight);
Create an ImageIcon using the BufferedImage and add the Icon to a JLabel. Then you can just add a Border to the label to get your desired padding.
To defer centering until rendering, I like this approach due to finnw, where this is a suitable component:
private BufferedImage image;
....
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.translate(-image.getWidth() / 2, -image.getHeight() / 2);
g2d.drawImage(image, 0, 0, null);
}