|
I'm trying to make a command-line java app for batch imaging cropping..
I tried searching the net for examples, but it seems there aren't any for batch processing..
I don't want a GUI.. just a simple one..
I did get one example;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Crop extends JFrame {
Image image;
Insets insets;
public Crop() {
super();
ImageIcon icon = new ImageIcon("java2s.PNG");
image = icon.getImage();
image = createImage(new FilteredImageSource(image.getSource(),
new CropImageFilter(73, 63, 141, 131)));
}
public void paint(Graphics g) {
super.paint(g);
if (insets == null) {
insets = getInsets();
}
g.drawImage(image, insets.left, insets.top, this);
}
public static void main(String args[]) {
JFrame f = new Crop();
f.setSize(200, 200);
f.show();
}
}
|
|
but this only crops it, and paints it to the app, it does not save it.. plus not sure on how to pass the args[] "/forum/pic1.png" "/forum/pic2.png" "/forum/pic3.png" x_point_to_crop, y_point_to_crop, crop_width, crop_height
any ideas?
|
|
|
This may help get you started.
There is a lot of information needed, viz, the member variables in the app below.
How to get the information into the app?
Some options that will avoid re-compiling each time:
1 - enter on the command line
2 - make a Reader and prompt-for/get input at the command line
3 - write out menu options to the consoles and prompt-for/get selections
4 - read the info from a file.
To use this make a folder in the current directory named "imagesToCrop" and put the images you want to crop inside it. Edit the member variables according to your desires, compile and run the app.
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class CommandCropping {
int x = 25;
int y = 25;
int width = 100;
int height = 100;
int type = BufferedImage.TYPE_INT_RGB; // many options
String srcPath = "imagesToCrop"; // parent folder
String destPath = "croppedImages"; // child folder
// Save images to file with this extension.
String ext = "jpg"; // png, bmp (j2se 1.5+), gif (j2se 1.6+) okay
private void cropImages() {
BufferedImage[] images = loadImages();
BufferedImage[] cropped = getCroppedImages(images);
save(cropped);
}
private BufferedImage[] loadImages() {
File folder = new File(srcPath);
if(!folder.exists()) {
System.out.println("Can't find the " + srcPath + " folder");
System.exit(1);
}
File[] files = folder.listFiles(new ImageFilter());
BufferedImage[] images = new BufferedImage[files.length];
try {
for(int j = 0; j < files.length; j++) {
images[j] = ImageIO.read(files[j]);
}
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
System.exit(1);
}
return images;
}
private BufferedImage[] getCroppedImages(BufferedImage[] images) {
BufferedImage[] crops = new BufferedImage[images.length];
for(int j = 0; j < images.length; j++) {
try {
BufferedImage image = images[j].getSubimage(x, y, width, height);
crops[j] = copy(image);
} catch(RasterFormatException e) {
System.out.println("RasterFormatException for image " + j +
": " + e.getMessage());
System.exit(1);
}
}
return crops;
}
private BufferedImage copy(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
// type is member variable
BufferedImage copy = new BufferedImage(w, h, type);
Graphics2D g2 = copy.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return copy;
}
private void save(BufferedImage[] images) {
File parent = new File(srcPath);
File folder = new File(parent, destPath);
if(!folder.exists())
folder.mkdir();
try {
for(int j = 0; j < images.length; j++) {
File file = new File(folder, "image " + (j+1) + "." + ext);
ImageIO.write(images[j], ext, file);
}
} catch(IOException e) {
System.out.println("Write error: " + e.getMessage());
System.exit(1);
}
}
private class ImageFilter implements FileFilter {
// String BMP = "bmp"; // j2se 1.5+ only
String GIF = "gif";
String JPG = "jpg";
String JPEG = "jpeg";
String PNG = "png";
public boolean accept(File file) {
String extension = getExtension(file);
if(extension == null) return false;
return extension.equals(GIF) || extension.equals(JPG) ||
extension.equals(JPEG) || extension.equals(PNG);
}
private String getExtension(File file) {
String s = file.getPath().toLowerCase();
int dot = s.lastIndexOf(".");
if(dot > 0 && dot < s.length()-1)
return s.substring(dot+1);
return null;
}
}
public static void main(String[] args) {
new CommandCropping().cropImages();
}
}
|
|
|
|
|
Here's the bare minimum. no ui/display. does a single image but you can tweak to do multiple if need be.
Example:
java Clippy 5 5 200 200 test.jpg test2.jpg jpg
arguments are:
x, y, height, width, inputfilename, outputfilename, type
semper fi...
import java.awt.image.BufferedImage;
import java.awt.image.RasterFormatException;
import javax.imageio.ImageIO;
import java.io.*;
public class Clippy
{
public static BufferedImage clipImage( BufferedImage image, int x, int y, int width, int height)
{
BufferedImage clipped = null;
try
{
clipped = image.getSubimage(x, y, width, height);
}
catch(RasterFormatException rfe)
{
System.out.println("raster format error: " + rfe.getMessage());
}
return clipped;
}
public static void main(String args[])
{
int x = 0;
int y = 0;
int w = 0;
int h = 0;
String input = null;
String output = null;
String type = null;
try
{
x = Integer.parseInt( args[0] );
y = Integer.parseInt( args[1] );
w = Integer.parseInt( args[2] );
h = Integer.parseInt( args[3] );
input = args[4];
output = args[5];
type = args[6];
BufferedImage image = ImageIO.read( new File(input) );
BufferedImage clipped = clipImage( image, x, y, w, h );
ImageIO.write( clipped, type, new File(output) );
}
catch (IOException ioe)
{
System.out.println("error: " + ioe.getMessage());
}
catch (Exception e)
{
System.out.println("error: " + e.getMessage());
}
}
}
|
|
|
wow, just got a chance to try out those code today, seems great,
I will try to intergrate the codes together so i can process multiple images with command line inputs..
thanks alot crwood and javabits..
<Added>
hmmm.. something strange..
when i use gif, and png images, the transparent layer turns black..??
any way of get back a transparent background??
Reference:
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)
|
|
|
For transparent background you can try
BufferedImage.TYPE_INT_ARGB_PRE
|
|
for the type in your BufferedImage.
Or you can try
int type = Bufferedimage.TYPE_INT_ARGB;
BufferedImage image = new BufferedImage(w, h, type)
Graphics2D g2 = image.createGraphics();
g2.setPaint(new Color(0,0,0,0)); // transparent
g2.fillRect(0,0,w,h);
// or you can try
g2.setBackground(new Color(0,0,0,0));
g2.clearRect(0,0,w,h);
|
|
|
|
|
worked like a charm.. thanks again..
|
|
|
|
|
|
|
// |