图像没有保存朱莉娅设置

时间:2014-03-01 21:56:40

标签: java image

所以我用Java创建了一个程序,目的是从一些给定的输入生成一个图像。我已经设法让程序编译和解析输入而不会崩溃,但它似乎没有保存输出图像。

以下是与保存文件有关的内容:

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;
}*/
}

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

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

要将图像保存到磁盘,可以使用以下方法:

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

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

这种方法非常方便使用。

答案 1 :(得分:0)

除了在您想要将图像写入文件时ImageIO可能更容易使用的事实之外,您当前尝试用于编写图像文件的代码并没有什么问题。

但剩下的就剩下了!

显然你做了很多反复试验。从代码中可以很快看到你并不真正知道你在做什么。你并没有真正计算任何东西,因为你在main方法中输入的值并没有真正用在Julia方法中,而且在Julia方法中完成的迭代是有缺陷的。

所以一些一般提示:

  • 遵循命名惯例
  • 向自己明确 您将使用 变量
  • 保持变量范围尽可能小。这意味着:在您想要使用它之前直接声明变量
  • 只要您还在尝试,请不要使用Scanner。只需为变量定义一些“默认”值,这样您每次启动程序时都不必反复输入它们!
  • 使用较小的图像尺寸。您不必等到计算出大的515x512图像才能看到它是否有效。 50 x50的图像就足够了。

稍微清理一下:

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;
    }
}