我試圖使用Java來裁剪圖像,這裏是我的代碼:的java croped圖像全黑
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");
}
}
}
一切運行正常(沒有錯誤),但是當我打開crop_pic.jpg
它是全黑的。這裏是pic.jpg。
我想知道爲什麼圖像全黑,以及如何修復它。
我想這
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");
}
}
}
,並得到這個錯誤:
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
謝謝Forseth11!應該注意到我沒有導入java.awt.image.ColorConvertOp!你一直在幫助很大。非常感謝!!!
Re。編譯器錯誤,請嘗試'導入''ColorConvertOp'('java.awt.image.ColorConvertOp')。 – haraldK