Java tutorial
/* * Copyright 2017 Mario Contreras <marioc@nazul.net>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package mx.iteso.desi.cloud.hw3; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.PutObjectRequest; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.logging.Level; import java.util.logging.Logger; import mx.iteso.desi.vision.ImagesMatUtils; import mx.iteso.desi.vision.WebCamStream; import org.opencv.core.Mat; public class FaceAddFrame extends javax.swing.JFrame { WebCamStream webCam; Mat lastFrame; AmazonS3 s3; public FaceAddFrame() { BasicAWSCredentials cred = new BasicAWSCredentials(Config.accessKeyID, Config.secretAccessKey); this.webCam = new WebCamStream(Config.CAMERA); initComponents(); this.s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(cred)) .withRegion(Config.amazonRegion).build(); // Center this.setLocationRelativeTo(null); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { photoPanel = new javax.swing.JPanel(); startButton = new javax.swing.JButton(); stopButton = new javax.swing.JButton(); uploadButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); nameTextField = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { closeWindow(evt); } public void windowClosing(java.awt.event.WindowEvent evt) { closeWindow(evt); } }); photoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Webcam")); photoPanel.setAutoscrolls(true); photoPanel.setLayout(new javax.swing.BoxLayout(photoPanel, javax.swing.BoxLayout.LINE_AXIS)); startButton.setText("Start"); startButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { startButtonActionPerformed(evt); } }); stopButton.setText("Stop"); stopButton.setEnabled(false); stopButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { stopButtonActionPerformed(evt); } }); uploadButton.setText("Upload"); uploadButton.setEnabled(false); uploadButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { uploadButtonActionPerformed(evt); } }); jLabel1.setText("Nombre:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(nameTextField) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(uploadButton)) .addComponent(photoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(stopButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addComponent(startButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(stopButton)) .addComponent(photoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(uploadButton).addComponent(jLabel1).addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed this.webCam.startStream(this.photoPanel); this.startButton.setEnabled(false); this.stopButton.setEnabled(true); this.uploadButton.setEnabled(false); }//GEN-LAST:event_startButtonActionPerformed private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed lastFrame = webCam.stopStream(); this.startButton.setEnabled(true); this.stopButton.setEnabled(false); this.uploadButton.setEnabled(lastFrame != null); }//GEN-LAST:event_stopButtonActionPerformed private void upload(String filename) { PutObjectRequest request = new PutObjectRequest(Config.srcBucket, "Faces/" + filename, new File(filename)); s3.putObject(request); } private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uploadButtonActionPerformed try { String filename = nameTextField.getText(); InputStream in = ImagesMatUtils.MatToInputStream(lastFrame); Files.copy(in, Paths.get(filename), StandardCopyOption.REPLACE_EXISTING); upload(filename); } catch (Exception ex) { Logger.getLogger(FaceAddFrame.class.getName()).log(Level.SEVERE, null, ex); } nameTextField.setText(""); lastFrame = null; uploadButton.setEnabled(false); }//GEN-LAST:event_uploadButtonActionPerformed private void closeWindow(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeWindow webCam.stopStream(); }//GEN-LAST:event_closeWindow // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JTextField nameTextField; private javax.swing.JPanel photoPanel; private javax.swing.JButton startButton; private javax.swing.JButton stopButton; private javax.swing.JButton uploadButton; // End of variables declaration//GEN-END:variables } // EOF