2009-03-13 5 views
2

그래서 나는 모든 것을 올리면서 아직 확실한 답을 얻지 못했습니다.어떻게 이미지를 Java로 오버레이 할 수 있습니까?

자르기 방법을 사용하여 이미지 크기 조정 클래스를 만들었습니다. 자르기가 잘됩니다. 내가 가지고있는 문제는 GraphicsdrawImage 함수에서 지정한 배경색이 올바르게 작동하지 않는다는 것입니다. 내가 제공하는 것 (이 경우에는 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); 
} 

답변

1

은 당신이 당신의 이미지를 엉망으로하는 그래픽 방법 또는 ImageIO에서 방법인지 분리 할 수 ​​있습니다 : 여기에 아래의 코드는 내가 함께 일하고있다? 이 단락 전체 ImageIO에서 프로세스를 간단하게 그 문제에

this.OutputImage = NewImage; 

를 할당, 나는 ImageIO에서의 조작에 의해 얻은 무언가가있다 생각하여이를 테스트 할 수있는 것 같습니다? 견본이 쓰여지면서, 그것은 (이상적으로) 아무런 행동도하지 않는 것처럼 보입니다.

또한 ImageIO 프로세스를 시작하기 전에 Graphics2D를 처리하지 마십시오. 그것은 종종 차이를 만들지 않지만, 당신은 그것을 추측하고 싶지 않습니다.

+0

답장을 보내 주셔서 감사합니다. 대단히 감사합니다. 격리 된 이상한 모든 오버 누워 물건을 제거하고 방금 새 버퍼링 된 이미지 및 setBackgroundColor 만든 this.OutputImage 할당 된 여전히 검정 배경이. 또한 clearify하기 위해 Graphics2D가 아닌 Graphics를 사용하고 있습니다. –

+0

새로운 코드는 어떻게 생겼습니까? 배경색만으로 설정해도 이미지는 변경되지 않습니다. 이는 클리어 연산 (clearRect, 아마도 Graphics2D 합성물)에서 사용됩니다. 여기 –

1

오버레이 색상 왜곡 문제에서 그래픽 컨텍스트가 페인트 모드이고 xor 모드가 아닌지 확인하십시오. (Graphics.setPaintMode()). 그렇지 않으면 색상 비트가 함께 XOR됩니다.

+0

솔루션이다 \t \t \t \t // 배경 설정 \t \t \t \t MyGraphics.setBackground (Color.decode (this.BackgroundColor)); \t \t \t \t \t \t \t \t // 분명히하는 구형 \t \t \t \t MyGraphics.clearRect (0, 0, 폭, 높이); 도움 주셔서 감사합니다. –

관련 문제