Reputation: 1430
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!!!
Upvotes: 0
Views: 1045
Reputation: 1438
I looked around a bit and found that other people have had a similar problem. On my end when testing this I got a strangely colored image, not a black image. This problem is caused because ImageIO
is reading the image wrong.
Here is what I have come up with which works, but since I could not replicate your problem and get a black image this may not work for you.
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);
String width = "" + rgbImage.getWidth();
String height = "" + rgbImage.getHeight();
System.out.println("heigth = " + height + " and width = " + width);
BufferedImage crp = rgbImage.getSubimage(300,300,rgbImage.getWidth()-300,rgbImage.getHeight()-300);
These are some other posts which have a similar issue:
Edit: I changed where it cropped, so it is easy to see because the upper left part of the image is mostly yellow.
Upvotes: 2