I am developing an application in swing which has 5 tabs with following 5 operations on an image :No Operation ,Color Convert ,Affine Transform ,Convolve and Look Up.
Here is the code :
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.awt.image.ShortLookupTable;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ImageProcessing extends JFrame{
BufferedImage source;
public static void main(String args[])
{
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e1){e1.printStackTrace();}
SwingUtilities.invokeLater(new Runnable(){public void run(){new ImageProcessing();}});
}
public ImageProcessing() {
// TODO Auto-generated constructor stub
super("Image Processing");
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
source=ImageIO.read(new File("src/abc.jpg"));
}catch(IOException e){System.out.println("Exception Here :"+e);}
JTabbedPane jtp=new JTabbedPane();
buildNoOpTab(jtp);
buildAffineTransformOpTab(jtp);
buildColorConvertOpTab(jtp);
buildConvolveOpTab(jtp);
buildLookUpOpTab(jtp);
//buildRescaleOpTab(jtp);
add(jtp);
setVisible(true);
}
private void buildNoOpTab(JTabbedPane jtp)
{
jtp.add("No Op",new JLabel(new ImageIcon(source)));
}
private void buildAffineTransformOpTab(JTabbedPane jtp)
{
BufferedImage dst;
AffineTransform transform=AffineTransform.getScaleInstance(0.5, 0.5);
AffineTransformOp op=new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
dst=op.filter(source, null);
jtp.add("AffineTransform",new JLabel(new ImageIcon(dst)));
}
private void buildColorConvertOpTab(JTabbedPane jtp)
{
BufferedImage dst = null;
ColorSpace clr=ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op=new ColorConvertOp(clr,null);
dst=op.filter(source,dst);
jtp.add("Color Convert",new JLabel(new ImageIcon(dst)));
}
private void buildConvolveOpTab(JTabbedPane jtp) {
BufferedImage dst = null;
float sharpen[] = new float[] {
0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f,
0.0f, -1.0f, 0.0f
};
Kernel kernel = new Kernel(3, 3, sharpen);
ConvolveOp op = new ConvolveOp(kernel);
dst = op.filter(source, null);
jtp.add("Convolve", new JLabel(new ImageIcon(dst)));
}
private void buildLookUpOpTab(JTabbedPane jtp)
{
BufferedImage dst=null;
short[] data=new short[256];
for(int i=0;i<256;i++)
data[i]=(short)(255-i);
LookupTable lkp=new ShortLookupTable(0,data);
LookupOp op=new LookupOp(lkp,null);
dst=op.filter(source, null);
jtp.add("Look Up",new JLabel(new ImageIcon(dst)));
}
}
There is some problem in the buildLookUpOpTab as removing this method application works fine.
Here is the exception which I am getting:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
Number of color/alpha components should be 3 but length of bits array is 1
at java.awt.image.ColorModel.<init>(ColorModel.java:336)
at java.awt.image.ComponentColorModel.<init>(ComponentColorModel.java:273)
at java.awt.image.LookupOp.createCompatibleDestImage(LookupOp.java:413)
at java.awt.image.LookupOp.filter(LookupOp.java:153)
at ImageProcessing.buildLookUpOpTab(ImageProcessing.java:108)
at ImageProcessing.<init>(ImageProcessing.java:49)
at ImageProcessing$1.run(ImageProcessing.java:30)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Can anyone tell me what is the problem in that method?
The LookupOp.filter method says that:
Performs a lookup operation on a BufferedImage. If the color model in the source image is not the same as that in the destination image, the pixels will be converted in the destination. If the destination image is null, a BufferedImage will be created with an appropriate ColorModel. An IllegalArgumentException might be thrown if the number of arrays in the LookupTable does not meet the restrictions stated in the class comment above, or if the source image has an IndexColorModel.
Since you are filtering a BufferedImage created from using ImageIO.read, the color model that the image will have will definitely not be IndexColorModel, since JPEGImageReader (which actually created the BufferdImage from the file) does not support IndexColorModel - in the olden days, JPEGs used the DirectColorModel
Have a look at the answer on this thread on how to read a JPEG file and use a different color model:
Unable to read JPEG image using ImageIO.read(File file)
You need to remove alpha channel from your image before using any filter on it. To make your code working, change:
try
{
source=ImageIO.read(new File("src/abc.jpg"));
} catch(IOException e){System.out.println("Exception Here :"+e);}
with this:
try
{
BufferedImage src = ImageIO.read(new File("abc.jpg"));
int w = src.getWidth();
int h = src.getHeight();
source = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Raster raster = src.getRaster().createChild(0, 0, w, h, 0, 0,
new int[] {0, 1, 2});
source.setData(raster);
}catch(IOException e){System.out.println("Exception Here :"+e);}
The above code creates a new buffered image in RGB mode and set the RGB data of original image to new buffered image ignoring the alpha values. But in case your original image contains completely transparent spots then it will become black spots in new buffered image.
Related
I am trying to merge two images in Java. The two photos must be positioned horizontally (the first to the left of the second).
I think I have problems in the write method of the ImageIO class.
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
public class Merge {
public static void main(String[] args) throws IOException {
String p = "../../Desktop/temp/";
BufferedImage left = ImageIO.read(new File(p+"006.jpg"));
BufferedImage right = ImageIO.read(new File(p+"007.jpg"));
BufferedImage imgClone = new BufferedImage(left.getWidth()+right.getWidth(), left.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D cloneG = imgClone.createGraphics();
cloneG.drawImage(right, 0, 0, null);
cloneG.drawImage(left, left.getWidth(), 0, null);
System.out.println(ImageIO.write(imgClone, "jpg", new File(p+"001.jpg"))); //always false
cloneG.dispose();
}
}
ImageIO.write(imgClone, "jpg", new File(p+"001.jpg")) always returns false, I think there is something wrong here but I can't figure out what.
If I go into debugging I can see the merged photo, but then it won't be saved in the folder.
I think it's because JPEG doesn't support transparency, and you used ARGB as the image buffer type. Removing the "A" worked for me.
class Merge {
public static void main(String[] args) throws IOException {
BufferedImage imgClone = new BufferedImage( 50, 50, BufferedImage.TYPE_INT_RGB);
// returns "true"
System.out.println(ImageIO.write(imgClone, "jpg", File.createTempFile( "Test-", "jpg")));
}
}
Edit I'm using Eclipse ADT bundle but when I try importing the library in the suggested answer above it doesn't work.
I'm using a combination of LibGDX and Java (90-99% java) to blur an image. The blur works but it takes nearly a second to take a screenshot, save it, access it, blur it, save it, and re-access it. I can't do much pre-processing because it takes a screenshot of the game to blur. Here is what I'm using currently for blurring: (I'll put drawing and screenshots up if you need to see them, but I think the issue lies in the blurring of an 800x480 png.)
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
public class Blur {
private static BufferedImage mshi;
private BufferedImage databuf;
private static int blurRad = 300;
public static void createBlur(FileHandle file) throws IOException {
mshi = ImageIO.read(file.file());
BufferedImage databuf = new BufferedImage(mshi.getWidth(null),
mshi.getHeight(null),
BufferedImage.TYPE_INT_BGR);
java.awt.Graphics g = databuf.getGraphics();
g.drawImage(mshi, 455, 255, null);
float[] blurKernel = new float[blurRad*blurRad];
for(int i =0; i<blurRad*blurRad; i++) {
blurKernel[i] = 1.0f/256.0f;
}
BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, blurKernel), ConvolveOp.EDGE_ZERO_FILL, null );
mshi = op.filter(mshi, databuf);
g.dispose();
File outputfile = Gdx.files.local("Blur.png").file();
ImageIO.write(mshi, "png", outputfile);
}
}
I have am writing a small game using Applet. I want to be able to check the color of a pixel on the screen. However, when I use .getRGB() on my buffered image in my game loop (a while loop that executes over and over), it gives me inconsistent values even if the pixel color never actually changes!
For example, if I fill the image with green color and I call .getRGB() on a pixel in the middle of the screen sometimes it gives me 0xFF00FF00 (green) as the color, other times it gives me 0xFF000000 (black) even if the color always stays green!
Any help?
Here's the relevant code if that helps, I've minimized it just to focus on the problem:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class t extends Applet implements Runnable {
Graphics2D bufferG;
BufferedImage bufferI;
final int WIDTH = 500, HEIGHT = 500;
public void init() {
setSize(WIDTH, HEIGHT);
bufferI = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
bufferG = bufferI.createGraphics();
(new Thread(this)).start();
}
public void run() {
while (true){
if (bufferI.getRGB(WIDTH/2, HEIGHT/2)==0xFF000000) System.out.println("BLACK");
}
}
public void paint(Graphics g) {
bufferG.setColor(Color.green);
bufferG.fillRect(0, 0, WIDTH, HEIGHT);
g.drawImage(bufferI, 0, 0, this);
}
}
I'm not sure why you are dealing with buffers. This paints the image green & produces no output on the command line (reporting the color as black).
// <applet code='t' width=400 height=200></applet>
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class t extends Applet implements Runnable {
BufferedImage bufferI;
final int WIDTH = 500, HEIGHT = 500;
public void init() {
bufferI = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = bufferI.getGraphics();
g.setColor(Color.GREEN);
g.fillRect(0,0,WIDTH,HEIGHT);
g.dispose();
(new Thread(this)).start();
}
public void run() {
while (true){
if (bufferI.getRGB(WIDTH/2, HEIGHT/2)==0xFF000000) System.out.println("BLACK");
}
}
public void paint(Graphics g) {
g.drawImage(bufferI, 0, 0, this);
}
}
Notes
To compile & run
prompt> javac t.java
prompt> appletviewer t.java // yes that file extension is correct.
Size
The size of an applet is set in the HTML that loads it, it should not attempt to set its own size.
I also need to find a library which allows to implement the "chroma key" effect in Java. The video contains some part in green color, which is replaced which a picture during the rendering in order to create a new video.
I am linking my question with a similar question which was already answered but with uncomplete answer (Looking for Chromakey library in Java). Could you please specify how did you do to have something up and working so quickly? I have been unsuccessful for some months fighting against the same issue.
c00kiemon5ter pointed several resources:
JavaCV
JAI (Java Advanced Imaging API)
Java Image Processing Cookbook
Which one did work for you?
JavaCV contains lots of methods to process video streams.
This code should get you started: http://tech.groups.yahoo.com/group/OpenCV/message/59118 but it's probably too slow in Java. Try this approach:
Create a filter which turns all the green pixels into a mask (look for things that "select by color").
Use the mask to copy the background image into the video.
I would like to contribute with a piece of code which gave me quite good results. I wonder if I used the classes and methods that Aaron Digulla suggested.
Please note that in this case my video has black background, that is why I am replacing the black color with the background image. I expect to obtain better results when I can edit the video to have green background, because black color is more likely to be used within the video main characters and replacing wrong pixels causes a quite awful effect.
--
package transparentvideo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import javax.media.Manager;
import javax.media.Player;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TransparentVideo
{
Player mediaPlayer;
JFrame invisibleFrame;
JFrame visibleFrame;
JLabel videoScreen;
JPanel offScreenVideo;
String backgroundImageFile="nature.jpg";
String videoFile="girl.mov";
public TransparentVideo()
{
invisibleFrame = new JFrame();
invisibleFrame.setSize(400, 400);
Container container=invisibleFrame.getContentPane();
offScreenVideo = getvidComp();
offScreenVideo.setPreferredSize(container.getSize());
offScreenVideo.setVisible(true);
container.add(offScreenVideo);
invisibleFrame.setVisible(true);
visibleFrame=new JFrame();
visibleFrame.setSize(container.getSize());
visibleFrame.setLocationRelativeTo(null);
videoScreen = new JLabel();
visibleFrame.getContentPane().add(videoScreen);
visibleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
visibleFrame.setVisible(true);
invisibleFrame.setVisible(false);
try
{
while(true)
{
if(mediaPlayer.getState()==Player.Started)
reDraw();
Thread.sleep(100);
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
public void reDraw()
{
BufferedImage bi=new BufferedImage(videoScreen.getWidth(), videoScreen.getHeight(),
BufferedImage.TYPE_INT_ARGB);
bi.getGraphics().drawImage(new ImageIcon(backgroundImageFile).getImage(), 0 , 0 ,
videoScreen.getWidth(), videoScreen.getHeight(), null);
BufferedImage screenShot = createImage((JComponent) offScreenVideo,
new Rectangle(invisibleFrame.getBounds()));
Image im = makeColorTransparent(new ImageIcon(screenShot).getImage(), Color.BLACK);
bi.getGraphics().drawImage(im, 0 ,0 , videoScreen.getWidth(), videoScreen.getHeight(), null);
videoScreen.setIcon(new ImageIcon(bi));
}
public static BufferedImage createImage(Component component, Rectangle region)
{
if (!component.isDisplayable()) {
Dimension d = component.getSize();
if (d.width == 0 || d.height == 0) {
d = component.getPreferredSize();
component.setSize(d);
}
layoutComponent(component);
}
BufferedImage image = new BufferedImage(region.width, region.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
if (!component.isOpaque())
{
g2d.setColor(component.getBackground());
g2d.fillRect(region.x, region.y, region.width, region.height);
}
g2d.translate(-region.x, -region.y);
component.paint(g2d);
g2d.dispose();
return image;
}
public static void layoutComponent(Component component)
{
synchronized (component.getTreeLock())
{
component.doLayout();
if (component instanceof Container)
{
for (Component child : ((Container) component).getComponents())
{
layoutComponent(child);
}
}
}
}
public JPanel getvidComp()
{
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,true);
try
{
mediaPlayer = Manager.createRealizedPlayer(new File(videoFile).toURL());
mediaPlayer.realize();
mediaPlayer.prefetch();
JPanel video=new JPanel(new BorderLayout());
video.add(mediaPlayer.getVisualComponent()) ;
mediaPlayer.start();
return video;
}
catch( Exception d)
{
return null;
}
}
public static Image makeColorTransparent( Image im, final Color color)
{
ImageFilter filter = new RGBImageFilter()
{
public int markerRGB = color.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb)
{
Color c=new Color(rgb);
int red=c.getRed();
int green=c.getGreen();
int blue=c.getBlue();
//if(red<140 && green<140 && blue<140)
{
int alpha=Math.max(Math.max(red , green), Math.max(green, blue))*3;
c=new Color(red , green , blue , alpha>255 ?255 :alpha );
}
return c.getRGB();
}
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
public static void main(String[] args) {
new TransparentVideo();
}
}
I have been playing with some of the imaging functionality in Java, trying to superimpose one image over another. Like so:
BufferedImage background = javax.imageio.ImageIO.read(
new ByteArrayInputStream(getDataFromUrl(
"https://www.google.com/intl/en_ALL/images/logo.gif"
))
);
BufferedImage foreground = javax.imageio.ImageIO.read(
new ByteArrayInputStream(getDataFromUrl(
"https://upload.wikimedia.org/wikipedia/commons/e/e2/Sunflower_as_gif_small.gif"
))
);
WritableRaster backgroundRaster = background.getRaster();
Raster foregroundRaster = foreground.getRaster();
backgroundRaster.setRect(foregroundRaster);
Basically, I was attempting to superimpose this: https://upload.wikimedia.org/wikipedia/commons/e/e2/Sunflower_as_gif_small.gif
on this: https://www.google.com/intl/en_ALL/images/logo.gif
The product appears as: http://imgur.com/xnpfp.png
From the examples I have seen, this seems to be the appropriate method. Am I missing a step? Is there a better way to handle this? Thank you for your responses.
Seems I was going about this in all the wrong ways. This solution outlined on the Sun forums works perfectly (copied here):
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
class TwoBecomeOne {
public static void main(String[] args) throws IOException {
BufferedImage large = ImageIO.read(new File("images/tiger.jpg"));
BufferedImage small = ImageIO.read(new File("images/bclynx.jpg"));
int w = large.getWidth();
int h = large.getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.drawImage(large, 0, 0, null);
g2.drawImage(small, 10, 10, null);
g2.dispose();
ImageIO.write(image, "jpg", new File("twoInOne.jpg"));
JOptionPane.showMessageDialog(null, new ImageIcon(image), "",
JOptionPane.PLAIN_MESSAGE);
}
}