89 lines
2.9 KiB
Java
89 lines
2.9 KiB
Java
package de.darkress.pixelfood;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.sound.sampled.Port;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.*;
|
|
import java.net.*;
|
|
import java.util.ArrayList;
|
|
|
|
class Main
|
|
{
|
|
private static BufferedImage processImage(String imageFile) {
|
|
BufferedImage image;
|
|
try {
|
|
File file = new File(imageFile);
|
|
image = ImageIO.read(file);
|
|
return image;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static ArrayList<String> prepareArray(BufferedImage image, int xOffset, int yOffset) {
|
|
ArrayList<String> pixelArray = new ArrayList<>();
|
|
|
|
int width = image.getWidth();
|
|
int height = image.getHeight();
|
|
|
|
// Iterate over each pixel
|
|
for (int y = 0; y < height; y++) {
|
|
for (int x = 0; x < width; x++) {
|
|
// Get the RGBA value of the pixel
|
|
int pixel = image.getRGB(x, y);
|
|
|
|
// Extract individual color components
|
|
int alpha = (pixel >> 24) & 0xFF;
|
|
int red = (pixel >> 16) & 0xFF;
|
|
int green = (pixel >> 8) & 0xFF;
|
|
int blue = pixel & 0xFF;
|
|
String rgbValue = String.format("%06X", (pixel & 0xFFFFFF));
|
|
String messageParameters = String.format("PX %s %s %s\n", x+xOffset, y+yOffset, rgbValue);
|
|
if(alpha != 0)
|
|
{
|
|
pixelArray.add(messageParameters);
|
|
}
|
|
|
|
// Print RGBA values
|
|
System.out.println("Pixel at (" + x + ", " + y + "):");
|
|
System.out.println(rgbValue);
|
|
}
|
|
}
|
|
return pixelArray;
|
|
}
|
|
|
|
//Syntax blabla.jar x y Hostname Port Image.png
|
|
public static void main(String[] args) {
|
|
final int PORT = Integer.parseInt(args[3]);;
|
|
final String HOSTNAME = args[2];
|
|
final int xOffset = Integer.parseInt(args[0]);
|
|
final int yOffset = Integer.parseInt(args[1]);
|
|
final String imageName = args[4];
|
|
|
|
InetAddress serverAddress = null;
|
|
try {
|
|
serverAddress = InetAddress.getByName(HOSTNAME);
|
|
} catch(UnknownHostException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// Read the PNG file
|
|
BufferedImage image = processImage(imageName);
|
|
ArrayList<String> pixelArray = prepareArray(image, xOffset, yOffset);
|
|
|
|
try {
|
|
DatagramSocket socket = new DatagramSocket();
|
|
while(true) {
|
|
for(int i = 0; i < pixelArray.size(); i++) {
|
|
byte[] messageBytes = pixelArray.get(0).getBytes();
|
|
DatagramPacket packet = new DatagramPacket(messageBytes, messageBytes.length, serverAddress, PORT);
|
|
socket.send(packet);
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|