create Array; send pixels

This commit is contained in:
Darkress
2023-06-09 04:47:11 +02:00
parent edf7384631
commit ff7ace05c6

View File

@@ -1,45 +1,79 @@
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 final int PORT = 1234;
private final String HOSTNAME = "localhost";
private byte[] message;
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) {
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, y, rgbValue);
if(alpha != 0)
{
pixelArray.add(messageParameters);
}
// Print RGBA values
System.out.println("Pixel at (" + x + ", " + y + "):");
System.out.println(rgbValue);
}
}
return pixelArray;
}
public static void main(String[] args) {
final int PORT = 1234;
final String HOSTNAME = "localhost";
InetAddress serverAddress = null;
try {
// Read the PNG file
File file = new File("uc.png");
BufferedImage image = ImageIO.read(file);
serverAddress = InetAddress.getByName(HOSTNAME);
} catch(UnknownHostException e) {
e.printStackTrace();
}
// Get image dimensions
int width = image.getWidth();
int height = image.getHeight();
// Read the PNG file
BufferedImage image = processImage("girl.png");
ArrayList<String> pixelArray = prepareArray(image);
// 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;
// Print RGBA values
/*System.out.println("Pixel at (" + x + ", " + y + "):");
System.out.println(" Alpha: " + alpha);
System.out.println(" Red: " + red);
System.out.println(" Green: " + green);
System.out.println(" Blue: " + blue);*/
System.out.println(Integer.toHexString(pixel));
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) {