WebcamUploader.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package data.io;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStream;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import java.util.Hashtable;
  14. import javax.imageio.ImageIO;
  15. import com.google.zxing.BarcodeFormat;
  16. import com.google.zxing.EncodeHintType;
  17. import com.google.zxing.WriterException;
  18. import com.google.zxing.common.BitMatrix;
  19. import com.google.zxing.qrcode.QRCodeWriter;
  20. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  21. public class WebcamUploader {
  22. public static void takePictureAndUpload(int id) throws IOException{
  23. Runtime.getRuntime().exec("streamer -c /dev/video0 -b 16 -o" + System.getProperty( "user.home" ) + "/ColorStrike/picture.jpeg");
  24. HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL("http://178.62.254.153/colorstrike/images/photoupload.php?filename="+id+".jpeg").openConnection();
  25. httpUrlConnection.setDoOutput(true);
  26. httpUrlConnection.setRequestMethod("POST");
  27. OutputStream os = httpUrlConnection.getOutputStream();
  28. try {
  29. Thread.sleep(2000);
  30. } catch (InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. FileInputStream fis = new FileInputStream(System.getProperty( "user.home" ) +"/ColorStrike/picture.jpeg");
  34. byte[] buffer = new byte[2048];
  35. int bytesRead;
  36. while ((bytesRead = fis.read(buffer)) != -1)
  37. {
  38. os.write(buffer, 0, bytesRead);
  39. }
  40. os.close();
  41. BufferedReader in = new BufferedReader(
  42. new InputStreamReader(
  43. httpUrlConnection.getInputStream()));
  44. String s = null;
  45. while ((s = in.readLine()) != null) {
  46. System.out.println(s);
  47. }
  48. in.close();
  49. fis.close();
  50. }
  51. public static BufferedImage createQRImage(String qrCodeText, int size) throws WriterException, IOException {
  52. // Create the ByteMatrix for the QR-Code that encodes the given String
  53. Hashtable hintMap = new Hashtable();
  54. hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
  55. QRCodeWriter qrCodeWriter = new QRCodeWriter();
  56. BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText,
  57. BarcodeFormat.QR_CODE, size, size, hintMap);
  58. // Make the BufferedImage that are to hold the QRCode
  59. int matrixWidth = byteMatrix.getWidth();
  60. BufferedImage image = new BufferedImage(matrixWidth, matrixWidth,
  61. BufferedImage.TYPE_INT_RGB);
  62. image.createGraphics();
  63. Graphics2D graphics = (Graphics2D) image.getGraphics();
  64. graphics.setColor(Color.WHITE);
  65. graphics.fillRect(0, 0, matrixWidth, matrixWidth);
  66. // Paint and save the image using the ByteMatrix
  67. graphics.setColor(Color.BLACK);
  68. for (int i = 0; i < matrixWidth; i++) {
  69. for (int j = 0; j < matrixWidth; j++) {
  70. if (byteMatrix.get(i, j)) {
  71. graphics.fillRect(i, j, 1, 1);
  72. }
  73. }
  74. }
  75. return image;
  76. }
  77. }