2017-08-05 7 views
-2
  1. ピクセルマトリックスを画像に変換しようとしていますが、機能しません。誰でも助けてくれますか?以下はコードです。ここで私はそれが実行されているかどうかを確認するためにprint文を追加しましたが、実行もしていません。誰でも私をここで助けることができます
  2. これは問題を起こしているコードです。
  3. これは完全なコードです。私はここに完全なコードを貼り付けている@DontKnowMuchButGettingBetter

画像をピクセル配列から変換する

import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.awt.image.ImageObserver; 
import java.awt.image.PixelGrabber; 
import java.awt.image.WritableRaster; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public final class Util { 
    /** 
    * Converts a java.awt.Image into an array of pixels 
    */ 
    public static int[] convertToPixels(Image img) { 
     int width = img.getWidth(null); 

     int height = img.getHeight(null); 
     int[] pixel = new int[width * height]; 

     PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width); 
     try { 
      pg.grabPixels(); 
     } catch (InterruptedException e) { 
      throw new IllegalStateException("Error: Interrupted Waiting for Pixels"); 
     } 
     if ((pg.getStatus() & ImageObserver.ABORT) != 0) { 
      throw new IllegalStateException("Error: Image Fetch Aborted"); 
     } 
     return pixel; 
    } 

    public static Image getImageFromArray(int[] pixels, int width, int height) throws IOException { 
     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
     WritableRaster raster = (WritableRaster) image.getData(); 
     raster.setPixels(0, 0, width, height, pixels); 
     File output = new File("C:\\out.png"); 
     ImageIO.write(image, "png", output); 
     System.out.print("written"); 
     return image; 
    } 

    public static void main(String args[]) throws IOException { 
     int width, height; 
     BufferedImage source = ImageIO.read(new File(args[0])); 
     width = source.getWidth(); 
     height = source.getHeight(); 
     // Util obj = new Util(); 
     Util.getImageFromArray(convertToPixels(source), width, height); 
    } 
} 
+0

。アウトプットが出てこない、つまりどこにでもout.pngという名前のファイルが作成されていない。 –

+0

あなたの主な目標は何ですか?ファイルからイメージを作成しますか? – Nikolay

+0

はい、私はピクセル配列を画像に変換して、それを@Nikolayに書き込もうとしました –

答えて

0
public static Image getImageFromArray(int[] pixels, int width, int height) throws IOException { 
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
    WritableRaster raster = image.getRaster(); //faster - no copy 
    raster.setDataElements(0, 0, width, height, pixels); //instead of setPixels 
    File output = new File("C:\\out.png"); 
    ImageIO.write(image, "png", output); 
    System.out.print("written"); 
    return image; 
} 
+0

本当にありがとう、ありがとう。 –

+0

ですので、回答には合格とマークしてください。 – Nikolay

関連する問題