2011-05-06 73 views
0

我試圖旋轉和圖像的圖像結果左,右90度。旋轉腐敗

出於某種原因,儘管在腐敗這個過程的輸出結果。

這裏是我的代碼:
(它的常規,但它並不需要多大的想象就假裝它的Java)

void rotate(File file){ 
    def image = ImageIO.read(file); 
    double theta = Math.PI/2; 
    def w = image.width/2; 
    def h = image.height/2; 

    def transform = new AffineTransform(); 
    transform.rotate(theta, h, w); 
    def op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); 
    image = op.filter(image, null); 

    def name = file.getName(); 
    def type = name.substring(name.lastIndexOf(".") + 1, name.length()); 
    ImageIO.write(image,type,file); 
} 

原: enter image description here

旋轉:enter image description here

回答

1

如果腐敗你指的是顏色的變化,拿出過濾器。如果我正確理解語法,那會給你一個負面的形象。

每當我使用轉換我離開過濾掉,並用手做他們。這確實需要很長時間,但它們總是變得更有用。只是一個建議。

+0

我真的不知道我在這裏做,但是在丟棄過濾器,做'g.drawImage(origImg,transorm,NULL);'提請正確的東西爲G – mkoryak 2011-05-06 15:07:09

+0

@mkyorak的方式你應用濾鏡基本上是說採取一個像素的顏色,並乘以(是默認的?)這些值由相同的像素顏色。這樣做會導致「負面」的形象。例如,葉子最暗的部分變得非常淺藍色。 – Jon 2011-05-06 19:00:05

1

filter()方法需要srcdstBufferedImage,它們必須不同。

Image image = null; 
try { 
    image = ImageIO.read(new File("gZtC3.jpg")); 
} catch (IOException ex) { 
    ex.printStackTrace(System.err); 
} 
double theta = Math.PI/2; 
int w = image.getWidth(null); 
int h = image.getHeight(null); 
AffineTransform at = AffineTransform.getRotateInstance(theta, w/2, h/2); 
BufferedImage src = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
Graphics2D g = src.createGraphics(); 
g.drawImage(image, 0, 0, null); 
g.dispose(); 
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC); 
BufferedImage dst = op.filter(src, null); 
this.add(new JLabel(new ImageIcon(dst), JLabel.CENTER)); 
+0

感謝您的代碼,但其實這有同樣的問題,也是在你的榜樣,你還在空傳爲第ARG過濾這是我做的。這似乎創建了另一個帶有原始內容的緩衝圖像,並寫入其中。 – mkoryak 2011-05-06 15:05:29

+0

您的源尚未呈現呢,所以它的旋轉而空。使用filter()對鏈接很好,但你接受的方法是正確的。比較這[Q&A](http://stackoverflow.com/questions/5864490/how-to-setsize-of-image-using-rescaleop/5864503)。 – trashgod 2011-05-06 18:01:53