How to bring 2 image for connect together (not merge) - java

http://image.ohozaa.com/view/6fcjh
How to do this with Java Source code
I can merge that
but i can not to connect that like this pictue

Another approach would be to combine multiple Icons into a single Icon. See Compound Icon.

Put each image in an ImageIcon, then each Icon in a JLabel, and then add both JLabels to a JPanel that uses GridLayout(2, 1) (2 rows, 1 column).

File path = new File("images");
BufferedImage image = ImageIO.read(new File(path, "1 (1).jpg"));
BufferedImage overlay = ImageIO.read(new File(path, "1 (2).jpg"));
int w = image.getWidth();
int h = image.getHeight()+ overlay.getHeight();
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, image.getHeight(), null);
ImageIO.write(combined, "PNG", new File(path, "combined 2.png"));

Related

Image quality reduces (turns reddish) as JPEG with BufferedImage

I am making a program where I extract out pixel array from an image, Take out ARGB values. And write them back again to make another image.
BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
iw = imagebuffer.getWidth();
ih = imagebuffer.getHeight();
pixels = new int[iw * ih];
PixelGrabber pg = new PixelGrabber(imagebuffer, 0, 0, iw, ih, pixels, 0, iw);
pg.grabPixels();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, pixels, 0, width);
ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));
Now output image for png and gif look fine but the output jpg image turns out quite reddish.
This is the original image
And this is the output jpg image
Any idea what might be causing this? Any push towards the right direction will be appreciated.
I dont know if this will work for you, but I always did it pixel-by-pixel.
So :
BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
iw = imagebuffer.getWidth();
ih = imagebuffer.getHeight();
BufferedImage image = new BufferedImage(iw,ih,BufferedImage.TYPE_INT_ARGB);
for (int x=0; x < iw; x++) {
for (int y=0; y < ih; y++) {
image.setRGB(x,y,imagebuffer.getRGB(x,y));
}
}
ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));
And it this way has a similar count of lines, so I think you could give it a try.
If you'd like to insert text, id ìmport java.awt.*;, what includes Graphics and Graphics2D and Font, and then :
Font font=new Font("Sans,0,20); //Name, type(none, bold, italic), size
Graphics2D imagegraphics=imagebuffer.createGraphics();
imagegraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //enable antialiasing
imagegraphics.setFont(font);
imagegraphics.setColor(Color.BLACK);
String yourtext="Fighter";
int h=imagegraphics.getFontMetrics().getHeight();
int w=imagegraphics.getFontMetrics().stringWidth(yourtext);
imagegraphics.drawString(yourtext,5,h); //Draw text upper left corner, note that y-value is the bottom line of the string
EDIT :
It was the Alpha value. Fixed by :
BufferedImage image = new
BufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB); //RGB, jpeg hasnt got alpha, ints have been converted as if they contain red first, but its alpha(the first bytes, these ints are interpreted bitwise i think) (argb), so it became more red.

Convert color image into black and white using java created from scratch

I want to convert my picture from colored to Black and white which seems to be created from scratch.
Here is the code which i tried as described on the different post:
BufferedImage bi = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
ImageIO.write(bi, "PNG", new File("/Users/bng/Documents/rendered2.png"));
op.filter(bi, bi);
But still my image is not converted to the Black and white. Additionally, this code is increasing the rendered2.png image size to 10 folds.
Also, it would be great if i could find some Java 8 way of doing this.
Any suggestions?
Here is the code which worked for me:
BufferedImage input = ImageIO.read(new File("/Users/bng/Documents/Photograph.jpg"));
// Create a black-and-white image of the same size.
BufferedImage im = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
// Get the graphics context for the black-and-white image.
Graphics2D g2d = im.createGraphics();
// Render the input image on it.
g2d.drawImage(input, 0, 0, null);
// Store the resulting image using the PNG format.
ImageIO.write(im, "PNG", new File("/Users/bng/Documents/rendered.png"));
It was BufferedImage.TYPE_BYTE_BINARY which provided me the exact solution.
Lokking for the Java 8 Version for above code.
You have to find RGB of the existing colors of the image you want to change it.
Fyi, you want to change it as white RGB value is (255,255,255) and for black RGB value is (0,0,0)
Following method easily do the color change if you apply correct way of your requirement
private BufferedImage changeColor(BufferedImage image, int srcColor, int replaceColor)
{
BufferedImage destImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = destImage.createGraphics();
g.drawImage(image, null, 0, 0);
g.dispose();
for (int width = 0; width < image.getWidth(); width++)
{
for (int height = 0; height < image.getHeight(); height++)
{
if (destImage.getRGB(width, height) == srcColor)
{
destImage.setRGB(width, height, replaceColor);
}
}
}
return destImage;
}
you have to use the ColorConvertOp in a proper way:
create Source image
apply filter
save dest
example:
BufferedImage src = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage dest = op.filter(src, null);
ImageIO.write(dest, "PNG", new File("/Users/bng/Documents/rendered2.png"));
src:
dest:

changing the size of ImageIcon

I have a Jtable. In my Jtable, the first column, I manage to import an image from my directory path store in a text file.
model.addRow(new Object[]{
new ImageIcon(value[1]),
value[2],
value[0],
value[3],
value[4],
});
But the image size is too large that it only show part of the image in the limited space in the table column.
How do I resize the image to height = 100 and Width = 100.
Load your Image first, then do
Image scaledIm = orignalIm.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(scaledIm);
Or use another rendering hints value
Resizing the icon is not straightforward. You need to use Java's graphics 2D to scale the image.
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}

Find format of clipboard image in Java

I'm getting images from clipboard using this:
if(Toolkit.getDefaultToolkit().getSystemClipboard().isDataFlavorAvailable(DataFlavor.imageFlavor)){
ImageIcon IMG = new ImageIcon((BufferedImage) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.imageFlavor));
}
Now I want to save this image in disk using ImageIO.write;
How can I find image format (JPG,PNG,GIF,...) to use in ImageIO.write as formatName ?
Thanks
The mime type of the content of the clipboard when checked via
.isDataFlavorAvailable(DataFlavor.imageFlavor)
is image/x-java-image (but OS vendors do not need to follow MIME types for clipboards).
I found two ways to supposedly get an image from a clipboard and write it to a file:
Using a helper method found in this blog post: The nightmares of getting images from the Mac OS X clipboard using Java.
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard()
ImageIcon IMG = new ImageIcon((BufferedImage)
clip.getData(DataFlavor.imageFlavor));
BufferedImage bImage = getBufferedImage(IMG.getImage());
ImageIO.write(bImage, "png", new File("/tmp/test.png"));
The getBufferedImage method looks like this:
public static BufferedImage getBufferedImage(Image img) {
if (img == null) {
return null;
}
int w = img.getWidth(null);
int h = img.getHeight(null);
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage bufimg = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufimg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return bufimg;
}
Via Transferable. Note that this runs on OS X but produces an empty image of the correct size:
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard()
Transferable content =
clip.getContents(null);
BufferedImage img = (BufferedImage) content.getTransferData(
DataFlavor.imageFlavor);
ImageIO.write(img, "png", new File("/tmp/test.png"));

How to resize tiff image in java?

I want to resize a .tiff file. I have used JAI toolkit to resize different types of images. Here is what I have tried to implement:
int imageWidth = 330;
int imageHeight = 490;
BufferedImage tempImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = tempImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
graphics2D.dispose();
File outfile = new File("D:/Work/YoursGallery/output.tif");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
FileSeekableStream ss = new FileSeekableStream("D:/Work/YoursGallery/sample1.tif");
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, null);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setTileSize(tempImage.getWidth(), tempImage.getHeight());
TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec.createImageEncoder("tiff", out, param);
encoder.encode(dec.decodeAsRenderedImage());
out.close();
The image created is having same size as original image has. Can anyone please tell what is the issue?
Here is the sample tiff image which I am using to test it.
http://docs.google.com/fileview?id=0BxCDhEXNFvbeMTYyMGZmNDYtODhhNy00YWI3LTkxNDgtZTNhM2FhMjg5Y2Q3&hl=en&authkey=CPCEypgM
Thanks in advance.
That's because you are writing out tempImage which is still the original image.
graphics2D.drawImage(image, 0, 0, imageWidth, imageHeight, null);
change that to:
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
or change your other code to write out image instead of tempImage
--Edit--
OK Attempt 2. Maybe having the source and destination the same is daft.
BufferedImage bsrc = ImageIO.read(new File(src));
BufferedImage bdest =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bdest.createGraphics();
AffineTransform at =
AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
(double)height/bsrc.getHeight());
g.drawRenderedImage(bsrc,at);
Try that :)
1) You are writing tempImage inside itself:
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
Should be:
graphics2D.drawImage(originalImage, 0, 0, imageWidth, imageHeight, null);
2) You are writing the image that you just read (why are You reading it btw?):
encoder.encode(dec.decodeAsRenderedImage());
Should be:
encoder.encode(tempImage);

Categories