Added webcam support + QR code generator.
Some cleanup
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user