Archive for the ‘Java’ Category
Image Processing Using Java
Hi check this out to process image using Java …I thought its a tough one .but there are lot of standard functions available.
ImageProcess.java
package Image_Handling;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class ImageProcess extends JFrame implements ActionListener
{
BufferedImage image;
JMenu file,edit;
JMenuItem open,save,exit,invert,bright,sharp,blur,edge,rotate;
JMenuBar bar;
JPanel panel;
Box box = Box.createVerticalBox();
JFileChooser chooser;
public ImageProcess()
{
super(“Image processing”);
try
{
show();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel=new JPanel();
setContentPane(panel);
setSize(500,500);
file=new JMenu(“File”);
open=new JMenuItem(“Open”);
save=new JMenuItem(“Save As..”);
exit=new JMenuItem(“Exit”);
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
edit=new JMenu(“Edit”);
blur=new JMenuItem(“Blur”);
edit.add(blur);
sharp=new JMenuItem(“Sharpen”);
edit.add(sharp);
bright=new JMenuItem(“Brightness”);
edit.add(bright);
rotate=new JMenuItem(“Rotate”);
edit.add(rotate);
edge=new JMenuItem(“Edge Detect”);
edit.add(edge);
invert=new JMenuItem(“invert”);
edit.add(invert);
bar=new JMenuBar();
bar.add(file);
bar.add(edit);
setJMenuBar(bar);
open.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
invert.addActionListener(this);
//bright.addActionListener(this);
blur.addActionListener(this);
sharp.addActionListener(this);
rotate.addActionListener(this);
edge.addActionListener(this);
}
catch(Exception e)
{
System.out.println(“error:”+e.getMessage());
}
setContentPane(new JPanel()
{
public void paint(Graphics g)
{
g.drawImage(image,0,0,this);
}
});
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==exit)
{
System.exit(0);
}
if(ae.getSource()==save)
{
saveFile();
}
if(ae.getSource()==open)
{
openFile();
}
if(ae.getSource()==invert)
{
invertImage();
}
if(ae.getSource()==blur)
{
blurImage();
}
if(ae.getSource()==bright)
{
brightImage();
}
if(ae.getSource()==sharp)
{
sharpImage();
}
if(ae.getSource()==edge)
{
float[] elements =
{
0.0f, -1.0f, 0.0f,
-1.0f, 4.f, -1.0f,
0.0f, -1.0f, 0.0f
};
convolve(elements);
}
if(ae.getSource()==rotate)
{
if (image == null) return;
AffineTransform transform = AffineTransform.getRotateInstance(
Math.toRadians(5), image.getWidth() / 2, image.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform,
AffineTransformOp.TYPE_BILINEAR);
filter(op);
}
}
public void openFile()
{
try
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(“.”));
int r = chooser.showOpenDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();
String name = f.getName();
//image=ImageIO.read(getClass().getResource(name));
image=ImageIO.read(chooser.getSelectedFile());
box.add(new JLabel(new ImageIcon(image)));
panel.add(new JScrollPane(box));
setSize(image.getWidth(), image.getHeight());
setTitle(name);
}
catch(Exception e)
{
}
}
public void saveFile()
{
try
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(“.”));
int r = chooser.showSaveDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
File f = chooser.getSelectedFile();
String name=f.getName();
ImageIO.write(image,”jpg”,new File(name));
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
public void invertImage()
{
byte negative[] = new byte[256];
for (int i = 0; i < 256; i++) negative[i] = (byte) (255 – i);
ByteLookupTable table = new ByteLookupTable(0, negative);
LookupOp op = new LookupOp(table, null);
filter(op);
}
public void sharpImage()
{
float[] elements ={0.0f, -1.0f, 0.0f,-1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f};
convolve(elements);
}
public void brightImage()
{
float a = 1.1f;
float b = -20.0f;
RescaleOp op = new RescaleOp(a, b, null);
filter(op);
}
public void blurImage()
{
float weight = 1.0f / 9.0f;
float[] elements = new float[9];
for (int i = 0; i < 9; i++) elements[i] = weight;
convolve(elements);
}
private void filter(BufferedImageOp op)
{
if (image == null) return;
BufferedImage filteredImage
= new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
op.filter(image, filteredImage);
image = filteredImage;
repaint();
}
private void convolve(float[] elements)
{
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp op = new ConvolveOp(kernel);
filter(op);
}
public static void main(String args[])throws Exception
{
new ImageProcess();
}
}
Download Code
Audio Player Using Java
Hey check this out ….i hav not implemented full functionality player but i hav tried my best just to play……
Audio.java
package Audio_Player;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.applet.*;
public class Audio extends JFrame implements ActionListener,Runnable
{
JButton p,s;
AudioClip wavsong,t,j;
public Audio()throws Exception
{
super(“WavPlayer”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
show();
JPanel pa=new JPanel();
setContentPane(pa);
pa.setBackground(new Color(100,200,100));
setLayout(null);
setSize(200,100);
setResizable(false);
p=new JButton(“Play”);
s=new JButton(“stop”);
p.setBounds(10,10,80,20);
add(p);
s.setBounds(100,10,80,20);
add(s);
wavsong=Applet.newAudioClip(new URL(“File:”+”ringin.wav”));
j=Applet.newAudioClip(new URL(“File:”+”j.wav”));
t=Applet.newAudioClip(new URL(“File:”+”tada.wav”));
p.addActionListener(this);
s.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getActionCommand().equals(“Play”))
{
Thread.sleep(100);
j.play();
t.play();
wavsong.play();
}
else
{
wavsong.stop();
j.stop();
t.stop();
}
}
catch(Exception ae)
{
}
}
public void run()
{
}
public static void main(String args[])throws Exception
{
new Audio();
}
}
Screen shot using Java
Hi here is the program to take screen shot just like print screen in your windows.
Screenshot.java
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
public class Screenshot {
public static void main(String args[]) throws
AWTException, IOException {
// capture the whole screen
BufferedImage screencapture = new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
// Save as JPEG
File file = new File(“screencapture.jpg”);
ImageIO.write(screencapture, “jpg”, file);
// Save as PNG
// File file = new File(“screencapture.png”);
// ImageIO.write(screencapture, “png”, file);
}
}
Download Code
How To create A JAR File
Hi friends follow the steps carefully to create the jar file.
Consider the following example
//Here the main class name is Sample
class Sample
{
public static void main(String ar[])
{
———–
————
}
}
Step 1 : Create the text file that lists the main class name and save it as mainClass.txt
Main-Class:Sample
Note :After typing the above text press Enter so the file contains two lines one is text and the oteher is blank line.
Step 2 : Now go to command prompt and type
jar cmf mainClass.txt example.jar *.class
Step 3 :Jar file will be created sucessfully
