Added webcam support + QR code generator.
Some cleanup
This commit is contained in:
@@ -4,14 +4,9 @@ import image.Images;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
|
||||
public class Song implements Comparable<Song>{
|
||||
|
||||
private String title;
|
||||
|
||||
@@ -2,10 +2,6 @@ package control;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.swing.Timer;
|
||||
|
||||
|
||||
@@ -3,9 +3,7 @@ package control;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import control.button.ButtonHandler;
|
||||
import control.joystick.Joystick.Position;
|
||||
|
||||
@@ -7,8 +7,6 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import main.Window;
|
||||
|
||||
import com.pi4j.io.gpio.GpioController;
|
||||
import com.pi4j.io.gpio.GpioFactory;
|
||||
import com.pi4j.io.gpio.GpioPinDigitalInput;
|
||||
|
||||
@@ -92,14 +92,14 @@ public class SQLConnector
|
||||
return hsc;
|
||||
}
|
||||
|
||||
public void addHighscore(Song s, SongInstance si, String name, int currentScore) {
|
||||
public int addHighscore(Song s, SongInstance si, String name, int currentScore) {
|
||||
if(name.length() <= 5 && currentScore <= Integer.MAX_VALUE)
|
||||
{
|
||||
String query = "INSERT INTO highscore (username, score, date, songinstance) VALUES ('" + name.trim() + "', " + currentScore + ", CURDATE(), (SELECT id FROM songinstance WHERE difficulty='" + si.getDifficulty() + "' AND song=(SELECT id FROM song WHERE folder='" + s.getFolder() + "')))";
|
||||
//System.out.println(query);
|
||||
executeInsertQuery(query);
|
||||
return executeInsertQuery(query);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void addPlaydata(Song s, SongInstance si, long time, int enemies_missed, int enemies_hit, int buttons_pressed, int joystick_moved) {
|
||||
@@ -166,12 +166,19 @@ public class SQLConnector
|
||||
}
|
||||
}
|
||||
|
||||
private void executeInsertQuery(String query)
|
||||
private int executeInsertQuery(String query)
|
||||
{
|
||||
try{
|
||||
Statement s = myConn.createStatement();
|
||||
s.execute(query);
|
||||
s.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
|
||||
ResultSet rs = s.getGeneratedKeys();
|
||||
int id = 0;
|
||||
if(rs.next())
|
||||
{
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
s.close();
|
||||
return id;
|
||||
}
|
||||
catch( SQLException ex)
|
||||
{
|
||||
@@ -183,6 +190,7 @@ public class SQLConnector
|
||||
{
|
||||
System.out.println("getMeasurement: " + ex.getMessage());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Execute a custom query
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package data.io;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.qrcode.QRCodeWriter;
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
|
||||
public class WebcamUploader {
|
||||
public static void takePictureAndUpload(int id) throws IOException{
|
||||
Runtime.getRuntime().exec("streamer -c /dev/video0 -b 16 -o" + System.getProperty( "user.home" ) + "/ColorStrike/picture.jpeg");
|
||||
|
||||
HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL("http://178.62.254.153/colorstrike/images/photoupload.php?filename="+id+".jpeg").openConnection();
|
||||
httpUrlConnection.setDoOutput(true);
|
||||
httpUrlConnection.setRequestMethod("POST");
|
||||
OutputStream os = httpUrlConnection.getOutputStream();
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
FileInputStream fis = new FileInputStream(System.getProperty( "user.home" ) +"/ColorStrike/picture.jpeg");
|
||||
|
||||
byte[] buffer = new byte[2048];
|
||||
int bytesRead;
|
||||
while ((bytesRead = fis.read(buffer)) != -1)
|
||||
{
|
||||
os.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
os.close();
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
httpUrlConnection.getInputStream()));
|
||||
|
||||
String s = null;
|
||||
while ((s = in.readLine()) != null) {
|
||||
System.out.println(s);
|
||||
}
|
||||
in.close();
|
||||
fis.close();
|
||||
}
|
||||
|
||||
public static BufferedImage createQRImage(String qrCodeText, int size) throws WriterException, IOException {
|
||||
// Create the ByteMatrix for the QR-Code that encodes the given String
|
||||
Hashtable hintMap = new Hashtable();
|
||||
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
|
||||
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
||||
BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText,
|
||||
BarcodeFormat.QR_CODE, size, size, hintMap);
|
||||
// Make the BufferedImage that are to hold the QRCode
|
||||
int matrixWidth = byteMatrix.getWidth();
|
||||
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth,
|
||||
BufferedImage.TYPE_INT_RGB);
|
||||
image.createGraphics();
|
||||
|
||||
Graphics2D graphics = (Graphics2D) image.getGraphics();
|
||||
graphics.setColor(Color.WHITE);
|
||||
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
|
||||
// Paint and save the image using the ByteMatrix
|
||||
graphics.setColor(Color.BLACK);
|
||||
|
||||
for (int i = 0; i < matrixWidth; i++) {
|
||||
for (int j = 0; j < matrixWidth; j++) {
|
||||
if (byteMatrix.get(i, j)) {
|
||||
graphics.fillRect(i, j, 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -55,7 +55,7 @@ public class Window extends JFrame {
|
||||
ButtonHandler bth = new ButtonHandler();
|
||||
JoystickHandler jsh = new JoystickHandler();
|
||||
|
||||
SQLConnector sql = new SQLConnector();
|
||||
final SQLConnector sql = new SQLConnector();
|
||||
NetworkHandler ntw = new NetworkHandler("192.168.1.6", 1113, bth, jsh);
|
||||
|
||||
bth.setNetwork(ntw);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package model.gameState;
|
||||
|
||||
import image.Images;
|
||||
import image.Images.ImageType;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
@@ -10,11 +9,21 @@ import java.awt.RenderingHints;
|
||||
import java.awt.Transparency;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.VolatileImage;
|
||||
import java.awt.image.WritableRenderedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import com.google.zxing.WriterException;
|
||||
|
||||
import model.GameModel;
|
||||
import model.SongHandler;
|
||||
import model.objects.InfoPanel;
|
||||
import model.objects.highscore.HighscoreName;
|
||||
import net.glxn.qrgen.QRCode;
|
||||
import net.glxn.qrgen.image.ImageType;
|
||||
import control.GameStateManager;
|
||||
import control.GameStateManager.State;
|
||||
import control.button.ButtonEvent;
|
||||
@@ -22,14 +31,17 @@ import control.button.ButtonHandler;
|
||||
import control.joystick.JoystickEvent;
|
||||
import control.joystick.JoystickHandler;
|
||||
import data.io.SQLConnector;
|
||||
import data.io.WebcamUploader;
|
||||
|
||||
public class GameOverState extends GameState {
|
||||
|
||||
BufferedImage gameOver = Images.getImage(ImageType.gameover);
|
||||
BufferedImage gameOver = Images.getImage(Images.ImageType.gameover);
|
||||
VolatileImage background;
|
||||
Font textFont = new Font("OCR A Extended", Font.BOLD, 70);
|
||||
HighscoreName hsn;
|
||||
int image_x = 0;
|
||||
int image_x = 0;
|
||||
private boolean uploaded;
|
||||
private BufferedImage QRimage;
|
||||
|
||||
int frame;
|
||||
|
||||
@@ -43,6 +55,7 @@ public class GameOverState extends GameState {
|
||||
createBackground();
|
||||
ButtonHandler.getButton(1).setColor(GameModel.colors[0]);
|
||||
JoystickHandler.REPEAT = true;
|
||||
uploaded = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,7 +68,12 @@ public class GameOverState extends GameState {
|
||||
public void draw(Graphics2D g2) {
|
||||
g2.drawImage(background, 0, 0, 1280, 1024, null);
|
||||
g2.drawImage(gameOver.getSubimage(image_x, 0, 40, 26), 640-122, 400, 245, 130, null);
|
||||
hsn.drawName(g2);
|
||||
if(!uploaded)
|
||||
hsn.drawName(g2);
|
||||
else if(QRimage != null){
|
||||
g2.drawImage(QRimage, 400, 600, 400, 400, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -63,13 +81,31 @@ public class GameOverState extends GameState {
|
||||
//System.out.println("Name: "+hsn.getName());
|
||||
switch(e.getButton().getButtonID()){
|
||||
case 1:
|
||||
case 0:
|
||||
if(hsn.getName().trim().length() >= 3)
|
||||
{
|
||||
sql.addHighscore(sh.getCurrentSong(), sh.getCurrentSongInstance(), hsn.getName(), PlayState.currentScore);
|
||||
gsm.setState(State.MENU_STATE);
|
||||
if(!uploaded){
|
||||
int id = sql.addHighscore(sh.getCurrentSong(), sh.getCurrentSongInstance(), hsn.getName(), PlayState.currentScore);
|
||||
try {
|
||||
WebcamUploader.takePictureAndUpload(id);
|
||||
} catch (IOException e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
QRimage = WebcamUploader.createQRImage("http://portfolio.jancokock.me/colorstrike/message.php?id="+id, 400);
|
||||
} catch (WriterException | IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
uploaded = true;
|
||||
|
||||
System.out.println(id);
|
||||
}else{
|
||||
gsm.setState(State.MENU_STATE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case 2:
|
||||
gsm.setState(State.MENU_STATE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import java.awt.Transparency;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.VolatileImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import model.GameModel;
|
||||
@@ -31,9 +30,9 @@ import control.GameStateManager.State;
|
||||
import control.button.ButtonEvent;
|
||||
import control.button.ButtonHandler;
|
||||
import control.joystick.Joystick;
|
||||
import control.joystick.Joystick.Position;
|
||||
import control.joystick.JoystickEvent;
|
||||
import control.joystick.JoystickHandler;
|
||||
import control.joystick.Joystick.Position;
|
||||
import data.io.SQLConnector;
|
||||
|
||||
public class MenuState extends GameState {
|
||||
|
||||
@@ -3,7 +3,6 @@ package model.objects;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.GeneralPath;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import audio.Song;
|
||||
|
||||
Binary file not shown.
@@ -1,8 +1,6 @@
|
||||
package view;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
@@ -11,7 +9,6 @@ import java.awt.Toolkit;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import control.GameStateManager;
|
||||
import control.LedHandler;
|
||||
|
||||
public class GameView extends JPanel {
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user