Java 如何为我的GMMK键盘创建环境光?

Java 如何为我的GMMK键盘创建环境光?,java,keyboard,Java,Keyboard,所以我试图在键盘上显示屏幕的平均颜色。 这就是所谓的“Ambilight”,已经有一些代码可以让我知道屏幕的平均颜色,但是我一直在研究如何将这些颜色传达给我的键盘软件,这样它就可以改变我键盘的颜色。 下面是一些人的代码,他们尝试了同样的方法,但使用了LED条(Arduino)。 因为我对编程非常陌生,所以我不理解下面代码中的每一行。现在我想知道我必须改变什么,或者我必须添加什么才能使我的键盘发光。 这是我的键盘软件:([下载GMMK ISO键盘编辑器-Compact Size v2.0]) 导入

所以我试图在键盘上显示屏幕的平均颜色。 这就是所谓的“Ambilight”,已经有一些代码可以让我知道屏幕的平均颜色,但是我一直在研究如何将这些颜色传达给我的键盘软件,这样它就可以改变我键盘的颜色。 下面是一些人的代码,他们尝试了同样的方法,但使用了LED条(Arduino)。 因为我对编程非常陌生,所以我不理解下面代码中的每一行。现在我想知道我必须改变什么,或者我必须添加什么才能使我的键盘发光。 这是我的键盘软件:([下载GMMK ISO键盘编辑器-Compact Size v2.0])

导入gnu.io.CommPortIdentifier;
导入gnu.io.PortinUseeException;
导入gnu.io.SerialPort;
导入gnu.io.UnsupportedComOperationException;
导入java.awt.AWTException;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Rectangle;
导入java.awt.Robot;
导入java.awt.image.buffereImage;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.util.Enumeration;
公共类环境光{
//Arduino通信配置
公共静态最终整数数据率=9600;
公共静态最终整数超时=2000;
//下一次颜色计算之间的延迟,单位为[ms]
专用静态最终长延时=10;
//条带上的LED编号
公共静态最终整数LED_NUM=30;
//每个部分的LED数量
每个截面的公共静态最终整数LED=3;
//由于性能原因,我们将条带拆分为多个部分
公共静态最终整数部分=每个部分的LED数量/LED数量;
//屏幕分辨率
公共静态最终整数X_RES=1920;
公共静态最终整数Y_RES=1080;
//截面宽度和高度
公共静态最终int截面宽度=X截面/截面;
公共静态最终内部截面高度=Y分辨率;
//为了获得更好的性能,我们不计算每个像素,
//但是跳过其中一些
公共静态最终int SECT_SKIP=10;
//机器人从屏幕上读取数据
私人机器人;
//arduino通信
专用串口;
私有输出流输出;
/**
*init arduino通信
*/
私有void initSerial(){
//找到arduino连接的端口
CommPortIdentifier serialPortId=null;
枚举enumComm=CommPortIdentifier.getPortIdentifiers();
while(enumComm.hasMoreElements()&&serialPortId==null){
serialPortId=(CommPortIdentifier)enumComm.nextElement();
}
试一试{
serial=(SerialPort)serialPortId.open(this.getClass().getName(),
超时);
serial.setSerialPortParams(数据速率,SerialPort.DATABITS 8,
SerialPort.stoppits_1,SerialPort.PARITY_NONE);
}捕获(PortinUseeException |不支持的操作异常){
e、 printStackTrace();
}
}
/**
*初始化机器人
*/
私有void initRobot(){
试一试{
机器人=新机器人();
}捕获(awtexe){
e、 printStackTrace();
}
}
/**
*初始arduino输出
*/
私有void initOutputStream(){
试一试{
output=serial.getOutputStream();
}捕获(IOE异常){
e、 printStackTrace();
}
}
/**
*从屏幕上读取颜色
* 
*@return数组,带有将发送给arduino的颜色
*/
私有颜色[]getColors(){
BuffereImage屏幕=robot.createScreenCapture(新矩形(
新维度(X_RES,Y_RES));
颜色[]LED=新颜色[部分];
用于(int led=0;led<区段;led++){
BuffereImage section=screen.getSubimage(发光二极管*截面宽度,0,
截面宽度、截面高度);
颜色部分avgcolor=getAvgColor(部分);
led[发光二极管]=截面平均颜色;
}
返回LED;
}
/**
*计算截面的平均颜色
*/
专用颜色getAvgColor(BuffereImage imgSection){
int width=imgSection.getWidth();
int height=imgSection.getHeight();
int r=0,g=0,b=0;
int循环=0;
对于(int x=0;x
这一切都取决于键盘。有API吗?或者一些可以与之接口的控制器软件?你需要先研究一下。也许可以问。@johnymapp用户在Arqade上发布了这个问题,但它与主题无关。技术编程问题不在我们的讨论范围之内scope@Wondercricket好啊我只是觉得Arqade的人可能已经做了类似的事情。我的错。这一切都取决于键盘。有API吗?或者一些可以与之接口的控制器软件?你需要先研究一下。也许可以问。@johnymapp用户在Arqade上发布了这个问题,但它与主题无关。技术编程问题不在我们的讨论范围之内scope@Wondercricket好啊我只是觉得Arqade的人可能已经做了类似的事情。我的错。
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;

public class Ambilight {

//Arduino communication config
public static final int DATA_RATE = 9600;
public static final int TIMEOUT = 2000;

//delay between next color calculations in [ms]
private static final long DELAY = 10;

//leds number on strip
public static final int LEDS_NUM = 30;

//number of leds per section
public static final int LEDS_PER_SECTION = 3;

//we split strip to sections because of performance reasons
public static final int SECTIONS = LEDS_NUM / LEDS_PER_SECTION;

//screen resolution
public static final int X_RES = 1920;
public static final int Y_RES = 1080;

//sections width and height
public static final int SECT_WIDTH = X_RES / SECTIONS;
public static final int SECT_HEIGHT = Y_RES;

//for better performance we do not calculate every pixel,
//but skip some of them
public static final int SECT_SKIP = 10;

// robot to read the data from the screen
private Robot robot;

// arduino communication
private SerialPort serial;
private OutputStream output;
/**
 * init arduino communication
 */
private void initSerial() {
// find the port where teh arduino is connected
CommPortIdentifier serialPortId = null;
Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
while (enumComm.hasMoreElements() && serialPortId == null) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
}
try {
serial = (SerialPort) serialPortId.open(this.getClass().getName(),
TIMEOUT);
serial.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (PortInUseException | UnsupportedCommOperationException e) {
e.printStackTrace();
}
}
/**
 * init the robot
 */
private void initRobot() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
 * init arduino output
 */

private void initOutputStream() {
try {
output = serial.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
 * read colors from the screen
 * 
 * @return array with colors that will be send to arduino
 */
private Color[] getColors() {
BufferedImage screen = robot.createScreenCapture(new Rectangle(
new Dimension(X_RES, Y_RES)));
Color[] leds = new Color[SECTIONS];
for (int led = 0; led < SECTIONS; led++) {
BufferedImage section = screen.getSubimage(led * SECT_WIDTH, 0, 
SECT_WIDTH, SECT_HEIGHT);
Color sectionAvgColor = getAvgColor(section);
leds[led] = sectionAvgColor;
}
return leds;
}
/**
 * calculate average color for section
 */
private Color getAvgColor(BufferedImage imgSection) {
int width = imgSection.getWidth();
int height = imgSection.getHeight();
int r = 0, g = 0, b = 0;
int loops = 0;
for (int x = 0; x < width; x += SECT_SKIP) {
for (int y = 0; y < height; y += SECT_SKIP) {
int rgb = imgSection.getRGB(x, y);
Color color = new Color(rgb);
r += color.getRed();
g += color.getGreen();
b += color.getBlue();
loops++;
}
}
r = r / loops;
g = g / loops;
b = b / loops;
return new Color(r, g, b);
}
/**
 * Send the data to Arduino
 */
private void sendColors(Color[] leds) {
try {
output.write(0xff);
for (int i = 0; i < SECTIONS; i++) {
output.write(leds[i].getRed());
output.write(leds[i].getGreen());
output.write(leds[i].getBlue());
}
} catch (IOException e) {
e.printStackTrace();
}
}

//Main Loop
private void loop() {
while (true) {
Color[] leds = getColors();
sendColors(leds);
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Ambilight ambi = new Ambilight();
ambi.initRobot();
ambi.initSerial();
ambi.initOutputStream();
ambi.loop();
}
}