Commit 279dd385 authored by Andreas König's avatar Andreas König
Browse files

send data to display

parents
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
\ No newline at end of file
/org.eclipse.wst.common.component
/org.eclipse.core.resources.prefs
/org.eclipse.m2e.core.prefs
/org.eclipse.wst.common.project.facet.core.xml
/org.eclipse.jdt.apt.core.prefs
/org.springframework.ide.eclipse.boot.prefs
This diff is collapsed.
eclipse.preferences.version=1
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
formatter_profile=_cablecom
formatter_settings_version=12
sp_cleanup.add_default_serial_version_id=true
sp_cleanup.add_generated_serial_version_id=false
sp_cleanup.add_missing_annotations=true
sp_cleanup.add_missing_deprecated_annotations=true
sp_cleanup.add_missing_methods=false
sp_cleanup.add_missing_nls_tags=false
sp_cleanup.add_missing_override_annotations=true
sp_cleanup.add_missing_override_annotations_interface_methods=true
sp_cleanup.add_serial_version_id=false
sp_cleanup.always_use_blocks=true
sp_cleanup.always_use_parentheses_in_expressions=false
sp_cleanup.always_use_this_for_non_static_field_access=false
sp_cleanup.always_use_this_for_non_static_method_access=false
sp_cleanup.convert_to_enhanced_for_loop=false
sp_cleanup.correct_indentation=true
sp_cleanup.format_source_code=true
sp_cleanup.format_source_code_changes_only=false
sp_cleanup.make_local_variable_final=true
sp_cleanup.make_parameters_final=true
sp_cleanup.make_private_fields_final=true
sp_cleanup.make_type_abstract_if_missing_method=false
sp_cleanup.make_variable_declarations_final=true
sp_cleanup.never_use_blocks=false
sp_cleanup.never_use_parentheses_in_expressions=true
sp_cleanup.on_save_use_additional_actions=true
sp_cleanup.organize_imports=true
sp_cleanup.qualify_static_field_accesses_with_declaring_class=false
sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true
sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true
sp_cleanup.qualify_static_member_accesses_with_declaring_class=false
sp_cleanup.qualify_static_method_accesses_with_declaring_class=false
sp_cleanup.remove_private_constructors=true
sp_cleanup.remove_trailing_whitespaces=true
sp_cleanup.remove_trailing_whitespaces_all=true
sp_cleanup.remove_trailing_whitespaces_ignore_empty=false
sp_cleanup.remove_unnecessary_casts=true
sp_cleanup.remove_unnecessary_nls_tags=false
sp_cleanup.remove_unused_imports=true
sp_cleanup.remove_unused_local_variables=false
sp_cleanup.remove_unused_private_fields=true
sp_cleanup.remove_unused_private_members=false
sp_cleanup.remove_unused_private_methods=true
sp_cleanup.remove_unused_private_types=true
sp_cleanup.sort_members=true
sp_cleanup.sort_members_all=false
sp_cleanup.use_blocks=true
sp_cleanup.use_blocks_only_for_return_and_throw=false
sp_cleanup.use_parentheses_in_expressions=false
sp_cleanup.use_this_for_non_static_field_access=false
sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true
sp_cleanup.use_this_for_non_static_method_access=false
sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.bergturbenthal.display.test</groupId>
<artifactId>display</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>display</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vaadin.version>8.0.5</vaadin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
package ch.bergturbenthal.display.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DisplayApplication {
public static void main(String[] args) {
SpringApplication.run(DisplayApplication.class, args);
}
}
package ch.bergturbenthal.display.test.display;
public interface Display {
DrawContext createContext();
}
package ch.bergturbenthal.display.test.display;
import java.awt.Graphics2D;
public interface DrawContext {
Graphics2D getGraphics();
int getHeight();
int getWidth();
void show();
}
package ch.bergturbenthal.display.test.display.impl;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import ch.bergturbenthal.display.test.display.Display;
import ch.bergturbenthal.display.test.display.DrawContext;
import lombok.Cleanup;
public class RowDisplay implements Display {
private static final int WIDTH = 32 * 4;
private static final int HEIGHT = 32 * 2;
private final SocketAddress address;
public RowDisplay(final SocketAddress address) {
this.address = address;
}
@Override
public DrawContext createContext() {
final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics = image.createGraphics();
return new DrawContext() {
@Override
public Graphics2D getGraphics() {
return graphics;
}
@Override
public int getHeight() {
return HEIGHT;
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public void show() {
try {
final byte[] data = write(transformImage(image).getRaster());
@Cleanup
final DatagramSocket datagramSocket = new DatagramSocket();
final DatagramPacket packet = new DatagramPacket(data, data.length, address);
datagramSocket.send(packet);
} catch (final IOException e) {
throw new RuntimeException("Cannot show content", e);
}
}
};
}
private BufferedImage transformImage(final BufferedImage image) {
final BufferedImage outImage = new BufferedImage(8 * 32, 32, BufferedImage.TYPE_INT_RGB);
final Graphics2D outGraphics = outImage.createGraphics();
outGraphics.drawImage(image, 4 * 32, -32, null);
outGraphics.transform(AffineTransform.getQuadrantRotateInstance(2, image.getWidth() / 2, image.getHeight() / 2));
outGraphics.drawImage(image, 0, 32, null);
return outImage;
}
private byte[] write(final Raster image) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
// write header
final int rowdimension = image.getHeight();
final int columndimension = image.getWidth();
writer.write("P6");
writer.newLine();
writer.write((columndimension) + " " + (rowdimension));
writer.newLine();
writer.write("255");
writer.newLine();
writer.flush();
for (int row = 0; row < rowdimension; row++) {
final byte[] columnData = new byte[columndimension * 3];
for (int column = 0; column < columndimension; column++) {
columnData[column * 3] = (byte) image.getSample(column, row, 0);
columnData[column * 3 + 1] = (byte) image.getSample(column, row, 1);
columnData[column * 3 + 2] = (byte) image.getSample(column, row, 2);
// writer.write(image.getSample(column, row, 0) + " ");
// writer.write(image.getSample(column, row, 1) + " ");
// writer.write(image.getSample(column, row, 2) + "");
// if (column < columndimension - 1) {
// writer.write(" ");
// }
}
os.write(columnData);
// writer.newLine();
}
os.close();
return os.toByteArray();
}
}
package ch.bergturbenthal.display.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DisplayApplicationTests {
@Test
public void contextLoads() {
}
}
package ch.bergturbenthal.display.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Collections;
import javax.imageio.ImageIO;
import ch.bergturbenthal.display.test.display.DrawContext;
import ch.bergturbenthal.display.test.display.impl.RowDisplay;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TestCreatePPM {
public static void main(final String[] args) throws IOException {
final BufferedImage image = new BufferedImage(4 * 32, 2 * 32, BufferedImage.TYPE_INT_RGB);
final RowDisplay display = new RowDisplay(new InetSocketAddress(InetAddress.getByName("192.168.1.51"), 1337));
final DrawContext drawContext = display.createContext();
final Graphics2D graphics = drawContext.getGraphics();
final String text = "HelloWorld";
graphics.setFont(new Font(Font.SERIF, 0, 19));
graphics.setColor(Color.BLUE.darker().darker().darker());
graphics.setRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
final FontMetrics fontMetrics = graphics.getFontMetrics();
final int width = fontMetrics.stringWidth(text);
final int height = fontMetrics.getHeight();
final int x = image.getWidth() / 2 - width / 2;
final int y = image.getHeight() / 2 + height / 2 - fontMetrics.getDescent();
graphics.fillRect(0, y - fontMetrics.getAscent(), image.getWidth(), fontMetrics.getHeight());
graphics.setColor(Color.WHITE);
graphics.drawString(text, x, y);
final BufferedImage image2 = ImageIO.read(new File("/tmp/IMG_20170428_090123.jpg"));
graphics.drawImage(image2, 0, 0, drawContext.getWidth(), drawContext.getHeight(), null);
drawContext.show();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment