0
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.imageio.*;
import javax.swing.*;

public class CreateImage extends Component{

    BufferedImage img;

    int pixel = 265*190;
    int steg = 3;
    int p,s;
    int [][] rgb_steg = new int [pixel][steg];



     public CreateImage()throws Exception{


        try{
            img = ImageIO.read(new File("cover-fruits.jpg"));
        }catch (IOException e){

        }



        File f = new File("stego_rgb.txt");
        if(!(f.exists())){
            System.out.println("Sorry file are not exist");
            System.exit(0);
        }


        Scanner in = new Scanner(f);
        String data = null;
        int rgb = 0;
        int rgb1 = 0;
        p=0;


        while(in.hasNextInt()){
            data = in.nextLine();
            StringTokenizer st = new StringTokenizer(data," ");
            for(s=0;s<steg;s++){
                rgb = Integer.parseInt(st.nextToken());
                rgb_steg[p][s] = rgb;
                rgb1 = rgb_steg[p][s];
            }
            p++;


        }




         for(int y = 0 ; y <img.getHeight() ; y++){
               for(int x = 0; x < img.getWidth() ; x++){
                   img.setRGB(x, y, rgb1);
               }
           }





    File output = new File("saved.png");
    ImageIO.write(img , "png", output);

    img  = ImageIO.read(new File("saved.png"));



    }

       public static void main(String[] args) throws Exception{
        JFrame f = new JFrame("Load Image");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
});

        f.add(new CreateImage());
        f.pack();
        f.setVisible(true);
    }





}

我正在尝试从 .txt 读取 RGB 值并创建图像。但我没能做到。我对从像素值创建图像有一些想法, 但仍然无法得到它。希望任何人都可以帮助我。

4

1 回答 1

0

这是一个使用硬编码值创建图像的简单示例。使数组值的人口动态化不应该太难。

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class ImageFromArray2 extends JFrame
{
    int width = 50;
    int height = 50;
    int imageSize = width * height * 3;

    public ImageFromArray2()
    {
        JPanel panel = new JPanel();
        getContentPane().add( panel );
        int[] pixels = new int[imageSize];

        //  Create Red Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 255;
            pixels[i+1] = 0;
            pixels[i+2] = 0;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Green Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 255;
            pixels[i+2] = 0;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Blue Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 0;
            pixels[i+2] = 255;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Cyan Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 255;
            pixels[i+2] = 255;
        }

        panel.add( createImageLabel(pixels) );

    }

    private JLabel createImageLabel(int[] pixels)
    {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        WritableRaster raster = image.getRaster();
        raster.setPixels(0, 0, width, height, pixels);
        JLabel label = new JLabel( new ImageIcon(image) );
        return label;
    }

    public static void main(String args[])
    {
        JFrame frame = new ImageFromArray2();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
}

它使用一个简单的数组,但您需要 3 个插槽来指定每个像素的 RGB 值。

于 2013-06-21T05:23:10.793 回答