그래서 나는 모든 것을 올리면서 아직 확실한 답을 얻지 못했습니다.어떻게 이미지를 Java로 오버레이 할 수 있습니까?
자르기 방법을 사용하여 이미지 크기 조정 클래스를 만들었습니다. 자르기가 잘됩니다. 내가 가지고있는 문제는 Graphics
의 drawImage
함수에서 지정한 배경색이 올바르게 작동하지 않는다는 것입니다. 내가 제공하는 것 (이 경우에는 Color.WHITE
)과 관계없이 배경이 검은 색으로 기본 설정됩니다.
또한 오버레이 이미지 또는 파일에서 오는 최상위 이미지가 뒤집어지고 있거나 (그렇지 않다고 생각됩니다) 그렇지 않으면 변색되어 있습니다. 그래서 조금 더 잘 개념화 할 수 있습니다. jpeg를 가져 와서 새로운 BufferedImage
상단에 오버레이하여 새 버퍼링 된 이미지의 배경이 설정되지 않습니다.
public void Crop(int Height, int Width, int SourceX, int SourceY) throws Exception {
//output height and width
int OutputWidth = this.OutputImage.getWidth();
int OutputHeight = this.OutputImage.getHeight();
//create output streams
ByteArrayOutputStream MyByteArrayOutputStream = new ByteArrayOutputStream();
MemoryCacheImageOutputStream MyMemoryCacheImageOutputStream = new MemoryCacheImageOutputStream(MyByteArrayOutputStream);
//Create a new BufferedImage
BufferedImage NewImage = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB);
Graphics MyGraphics = NewImage.createGraphics();
MyGraphics.drawImage(this.OutputImage, -SourceX, -SourceY, OutputWidth, OutputHeight, Color.WHITE, null);
// Get Writer and set compression
Iterator MyIterator = ImageIO.getImageWritersByFormatName("png");
if (MyIterator.hasNext()) {
//get image writer
ImageWriter MyImageWriter = (ImageWriter)MyIterator.next();
//get params
ImageWriteParam MyImageWriteParam = MyImageWriter.getDefaultWriteParam();
//set outputstream
MyImageWriter.setOutput(MyMemoryCacheImageOutputStream);
//create new ioimage
IIOImage MyIIOImage = new IIOImage(NewImage, null, null);
//write new image
MyImageWriter.write(null, MyIIOImage, MyImageWriteParam);
}
//convert output stream back to inputstream
ByteArrayInputStream MyByteArrayInputStream = new ByteArrayInputStream(MyByteArrayOutputStream.toByteArray());
MemoryCacheImageInputStream MyMemoryCacheImageInputStream = new MemoryCacheImageInputStream(MyByteArrayInputStream);
//resassign as a buffered image
this.OutputImage = ImageIO.read(MyMemoryCacheImageInputStream);
}
답장을 보내 주셔서 감사합니다. 대단히 감사합니다. 격리 된 이상한 모든 오버 누워 물건을 제거하고 방금 새 버퍼링 된 이미지 및 setBackgroundColor 만든 this.OutputImage 할당 된 여전히 검정 배경이. 또한 clearify하기 위해 Graphics2D가 아닌 Graphics를 사용하고 있습니다. –
새로운 코드는 어떻게 생겼습니까? 배경색만으로 설정해도 이미지는 변경되지 않습니다. 이는 클리어 연산 (clearRect, 아마도 Graphics2D 합성물)에서 사용됩니다. 여기 –