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值并创建图像。但我没有设法做到这一点。 我有Creating Image from Pixel Values的一些想法 但仍然无法得到它。 希望有人能帮助我。
答案 0 :(得分: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值。