2014-03-01 70 views
0

所以我在Java中制作了一个程序,目的是从某些给定的输入生成图像。我设法让程序编译和解析输入而不会崩溃,但它似乎并没有保存输出图像。Image not saving Julia sets

这里的东西有关保存文件:

public static void saveImage(BufferedImage img, File file) throws IOException { 

    ImageWriter  writer = null; 
    java.util.Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); 

    if(iter.hasNext()){ 
     writer = (ImageWriter)iter.next(); 
    } 

    ImageOutputStream ios = ImageIO.createImageOutputStream(file); 
    writer.setOutput(ios); 

    ImageWriteParam param = new JPEGImageWriteParam(java.util.Locale.getDefault()); 
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; 
    param.setCompressionQuality(0.98f); 

    writer.write(null, new IIOImage(img, null, null), param); 

} 


public static void save(String[] args) throws Exception { 

BufferedImage colorImage, julia; 

if (args.length != 1) 
    System.out.println("usage is: java Julia filename"); 
else 
{ 

julia = Julia(); 

saveImage(julia, new File("julia" + args[0])); 

} 

,这里是我的代码,在它的全部:

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.net.URL; 
import java.util.Scanner; 
import java.lang.Math.*; 

import javax.imageio.IIOImage; 
import javax.imageio.ImageIO; 
import javax.imageio.ImageWriteParam; 
import javax.imageio.ImageWriter; 
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; 
import javax.imageio.stream.ImageOutputStream; 

public class Julia{ 


public static void saveImage(BufferedImage img, File file) throws IOException { 

    ImageWriter  writer = null; 
    java.util.Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); 

    if(iter.hasNext()){ 
     writer = (ImageWriter)iter.next(); 
    } 

    ImageOutputStream ios = ImageIO.createImageOutputStream(file); 
    writer.setOutput(ios); 

    ImageWriteParam param = new JPEGImageWriteParam(java.util.Locale.getDefault()); 
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; 
    param.setCompressionQuality(0.98f); 

    writer.write(null, new IIOImage(img, null, null), param); 

} 

public static BufferedImage Julia() 

{ 

//z= (Xmin +i(Xmax- Xmin) /size) + (Ymin+ j(Ymax-Ymin)/size); 
    BufferedImage outImage; 
    float zreal, zimag; 
    float cReal, cImag; 
    float xMin, xMax, yMin, yMax; 
    int height, width, size; 
    height = width = size = 512; 
    outImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 
    //initializing variables 
    cReal = 0; 
    cImag = 0; 
    xMin = 0; 
    xMax = 0; 
    yMin = 0; 
    yMax = 0; 

    //c= creal+cimag 
    Complex c; 
    c = new Complex (cReal, cImag); 



int i, j ,k; 
Complex f; 

for (i = 0; i<width; i++) 
    { 
    for(j = 0; j<height; j++) 
     { 
     for (k= 0; k< 256; k++) 
      { 
     zreal = (xMin +i*(xMax- xMin) /size); 
     zimag = (yMin+ j*(yMax-yMin)/size); 
     Complex z = new Complex(zreal, zimag); 

      //z = z^2 
      //f= z^2 +c 

     z = z.multi(z); 
     f = z.addd(c); 

     if (Math.sqrt(Math.pow(zreal, 2.0)+Math.pow(zimag, 2.0))>2) 
      break; 
      } 

    int pixel = getHSBColor(k); 
    outImage.setRGB(i, j, pixel); 
     } 

    } 

    return(outImage); 

} 

public static void main(String[] args) 

{ 
    BufferedImage outImage, julia; 
    double cReal, cImag; 
    float xMin, xMax, yMin, yMax; 

    Scanner scan = new Scanner (System.in); 
    System.out.println("Input: cReal cImag xMin xMax yMin yMax fname"); 
    cReal=scan.nextDouble(); 
    cImag=scan.nextDouble(); 
    xMin=scan.nextFloat(); 
    xMax=scan.nextFloat(); 
    yMin=scan.nextFloat(); 
    yMax=scan.nextFloat(); 
    String fname = scan.next(); 


    double f; 
    int z; 
    int c; 
    int height, width, size; 
    height = width = size = 512; 

    outImage = Julia(); // Put 6 junk inside cReal, cImag, xMin, xMax, yMin, yMax 

    } 

//something I tried to save the file that didn't seem to work properly// 
/* try {   
File outputfile = new File("Julia" + args[0]); 
ImageIO.write(julia, "jpg", outputfile); 
} catch (IOException e) { 

}*/ 
    public static void save(String[] args) throws Exception { 

BufferedImage colorImage, julia; 

if (args.length != 1) 
    System.out.println("usage is: java Julia filename"); 
else 
{ 

julia = Julia(); 

saveImage(julia, new File("julia" + args[0])); 

} 
} 
// hsb color table 
public static int getHSBColor(int idx) 
{ 
    return Color.getHSBColor((float)(idx/255.0), 1.0f, 1.0f).getRGB(); 
} 


} 

class Complex 
{ 
double real; 
double imaginary; 

Complex(double newReal, double newImaginary) 
{ 

    real = newReal; 
    imaginary = newImaginary; 
} 

public Complex addd(Complex complexToAdd) 
{ 
    double x , y , newreal, newimaginary; 

    newreal = complexToAdd.real; 
    newimaginary = complexToAdd.imaginary; 
    x = real + newreal; 
    y = imaginary + newimaginary; 
    Complex newComplex = new Complex(x, y); 

    return newComplex; 
} 

public Complex multi(Complex complexToMulti) 
{ 

    double x, y , newreal, newimaginary; 


    newreal = complexToMulti.real; 
    newimaginary = complexToMulti.imaginary; 

    x = real*newreal - imaginary*newimaginary; 
    y = real*newimaginary + imaginary*newreal; 

    Complex newComplex= new Complex(x, y); 

    return newComplex; 
} 


// extra absolute value method // 
/*public Complex abso (Complex complexAbso) { 
double x, y, newreal, newimiginary; 

real = complexAbso.real; 
imaginary= complexAbso.imaginary; 
x = real*real; 
y = imaginary*imaginary; 

Complex newComplex = new int (x, y); 

return newComplex; 
}*/ 
} 

人有什么想法?任何和所有的帮助将不胜感激。

谢谢你的时间。

回答

0

保存图像到磁盘,下面的方法可用于:

javax.imageio.ImageIO.write(Image, format, file); 

格式可以是 “JPG” 或 “PNG”。该方法的其他变体也是可用的。

这种方法非常方便使用。

0

除了当您要将图像写入文件时使用ImageIO可能更易于使用这一事实之外,您当前试图用于编写图像文件的代码没有出现问题。

但与其余所有!

显然你正在做很多反复试验。从代码中可以很快看出,你并不知道你在那里做什么。由于您在main方法中输入的值在Julia方法中未实际使用,并且在Julia方法中完成的迭代有缺陷,因此您没有计算任何内容。

因此,一些一般的提示:

  • 按照命名约定
  • 明确自己哪里您将使用变量
  • 保持变量的范围尽可能小。这意味着:在你想使用它之前直接声明变量
  • 只要你还在试验,就不要使用Scanner。只需为变量定义一些“默认”值,以便每次启动程序时都不必一次又一次地输入它们!
  • 使用较小的图像大小。您不必等到一个大的515x512图像被计算出来看看它是否有效。 50x50的图像就足够了。

稍微清理:

import java.awt.Color; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class Julia 
{ 
    public static BufferedImage julia() 
    { 
     int width = 200; 
     int height = 100; 
     BufferedImage outImage = 
      new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 

     Complex c = new Complex (cReal, cImag); 

     for (int i = 0; i<width; i++) 
     { 
      for(int j = 0; j<height; j++) 
      { 
       double zreal = (xMin +i*(xMax- xMin)/width); 
       double zimag = (yMin+ j*(yMax-yMin)/ height); 
       Complex z = new Complex(zreal, zimag); 

       int k=0; 
       for (k= 0; k< 256; k++) 
       { 
        z = z.multi(z); 
        z = z.add(c); 
//     if (Math.sqrt(Math.pow(z.real, 2.0)+ 
//      Math.pow(z.imaginary, 2.0))>2) 
//      break; 

        // MUCH faster: 
        if (z.real*z.real+z.imaginary*z.imaginary>4) 
         break; 

       } 
       int pixel = getHSBColor(k); 
       outImage.setRGB(i, j, pixel); 
      } 
     } 
     return outImage; 
    } 

    static double cReal, cImag; 
    static float xMin, xMax, yMin, yMax; 

    public static void main(String[] args) throws IOException 
    { 
//  Scanner scan = new Scanner (System.in); 
//  System.out.println("Input: cReal cImag xMin xMax yMin yMax fname"); 
//  cReal=scan.nextDouble(); 
//  cImag=scan.nextDouble(); 
//  xMin=scan.nextFloat(); 
//  xMax=scan.nextFloat(); 
//  yMin=scan.nextFloat(); 
//  yMax=scan.nextFloat(); 
//  String fname = scan.next(); 

     xMin = -2; 
     yMin = -1; 
     xMax = 2; 
     yMax = 1; 
     cReal = -0.742; 
     cImag = 0.1; 

     BufferedImage image = julia(); 

     ImageIO.write(image, "jpg", new File("Julia01.jpg")); 
     showImage(image); 
    } 

    public static int getHSBColor(int idx) 
    { 
     return Color.getHSBColor((float)(idx/255.0), 1.0f, 1.0f).getRGB(); 
    } 

    private static void showImage(final BufferedImage image) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       JFrame f = new JFrame(); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.getContentPane().add(new JLabel(new ImageIcon(image))); 
       f.pack(); 
       f.setLocationRelativeTo(null); 
       f.setVisible(true); 
      } 
     }); 
    } 


} 

class Complex 
{ 
    double real; 
    double imaginary; 

    Complex(double newReal, double newImaginary) 
    { 

     real = newReal; 
     imaginary = newImaginary; 
    } 

    public Complex add(Complex complexToAdd) 
    { 
     double newreal = complexToAdd.real; 
     double newimaginary = complexToAdd.imaginary; 
     double x = real + newreal; 
     double y = imaginary + newimaginary; 
     Complex newComplex = new Complex(x, y); 
     return newComplex; 
    } 

    public Complex multi(Complex complexToMulti) 
    { 
     double newreal = complexToMulti.real; 
     double newimaginary = complexToMulti.imaginary; 
     double x = real*newreal - imaginary*newimaginary; 
     double y = real*newimaginary + imaginary*newreal; 
     Complex newComplex= new Complex(x, y); 
     return newComplex; 
    } 
}