I am trying to crop an image using a java, here is my code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class crop
{
public static void main(String[] args)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("/Users/mathewlewis/desktop/pic.jpg"));
String width = "" + img.getWidth();
String height = "" + img.getHeight();
cout("heigth = " + height + " and width = " + width);
BufferedImage crp = img.getSubimage(0,0,100,200);
try {
File outputfile = new File("crop_pic.jpg");
ImageIO.write(crp, "jpg", outputfile);
}
catch (IOException e)
{
System.out.println("error");
}
}
catch (IOException e)
{
System.out.println("error");
}
}
}
Everything runs fine (no errors), but when I open crop_pic.jpg
it is all black. Here is pic.jpg.
I would like to know why the image comes out all black, and how I can fix it.
I tried this
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class crop
{
public static void main(String[] args)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("/Users/mathewlewis/desktop/pic.jpg"));
BufferedImage rgbImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(img, rgbImage);
BufferedImage crp = rgbImage.getSubimage(300,300,rgbImage.getWidth()-300,rgbImage.getHeight()-300);
try {
File outputfile = new File("crop_pic.jpg");
ImageIO.write(crp, "jpg", outputfile);
}
catch (IOException e)
{
System.out.println("error");
}
}
catch (IOException e)
{
System.out.println("error");
}
}
}
and got this error:
crop.java:16: error: cannot find symbol
ColorConvertOp op = new ColorConvertOp(null);
^
symbol: class ColorConvertOp
location: class crop
crop.java:16: error: cannot find symbol
ColorConvertOp op = new ColorConvertOp(null);
^
symbol: class ColorConvertOp
location: class crop
2 errors
Thank you Forseth11!! should have noticed that I didn't import java.awt.image.ColorConvertOp! You've been a great help. Thanks a lot!!!