2016-11-25 6 views
-2

現在、コードは特定のファイルに保存しますが、ファイルを選択したり作成したりできます。それをどうやってやるの?ファイルを作成してディレクトリに保存するコードを変更するには

-package application; 

import java.awt.image.BufferedImage; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.TimeUnit; 

import javax.imageio.ImageIO; 

import org.opencv.core.Mat; 
import org.opencv.imgproc.Imgproc; 
import org.opencv.videoio.VideoCapture; 

import javafx.embed.swing.SwingFXUtils; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.FileChooser; 

public class GuiControl { 
    // Max number of pics. 
    private final int MAX = 6; 
    // keep track of num of pics 
    private int num = 0; 
    // the FXML button 
    @FXML 
    private Button button; 
    // the FXML image view 
    @FXML 
    private ImageView currentFram; 
    // The FXML capture Button 
    @FXML 
    private Button capBtn; 
    // The FXML Image holder box. 
    @FXML 
    private HBox imgHolder; 
    @FXML 
    private Button clearBtn; 
    @FXML 
    private Button savebtn; 
    // a timer for acquiring the video stream 
    private ScheduledExecutorService timer; 
    // the OpenCV object that realizes the video capture 
    private VideoCapture capture = new VideoCapture(); 
    // a flag to change the button behavior 
    private boolean cameraActive = false; 
    // the id of the camera to be used 
    private static int cameraId = 0; 

    // Array List to hold a label of image. 
    private ArrayList<ImageView> imageList = new ArrayList<>(6); 

    // Image field to hold temporary image. 
    ImageView temp; 

    // Image used to resize 
    Image t; 

    private Mat frame; 

    /** 
    * The action triggered by pushing the button on the GUI 
    * 
    * @param event 
    *   the push button event 
    */ 
    @FXML 
    protected void startCamera(ActionEvent event) { 
     if (!this.cameraActive) { 
      // start the video capture 
      this.capture.open(cameraId); 

      // is the video stream available? 
      if (this.capture.isOpened()) { 
       this.cameraActive = true; 

       // grab a frame every 33 ms (30 frames/sec) 
       Runnable frameGrabber = new Runnable() { 

        @Override 
        public void run() { 
         // effectively grab and process a single frame 
         frame = grabFrame(); 
         // convert and show the frame 
         Image imageToShow = Utils.mat2Image(frame); 
         updateImageView(currentFram, imageToShow); 
        } 
       }; 

       this.timer = Executors.newSingleThreadScheduledExecutor(); 
       this.timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS); 

       // update the button content 
       this.button.setText("Stop Camera"); 
      } else { 
       // log the error 
       System.err.println("Impossible to open the camera connection..."); 
      } 
     } else { 
      // the camera is not active at this point 
      this.cameraActive = false; 
      // update again the button content 
      this.button.setText("Start Camera"); 

      // stop the timer 
      this.stopAcquisition(); 
     } 
    } 

    /** 
    * Get a frame from the opened video stream (if any) 
    * 
    * @return the {@link Mat} to show 
    */ 
    private Mat grabFrame() { 
     // init everything 
     Mat frame = new Mat(); 

     // check if the capture is open 
     if (this.capture.isOpened()) { 
      try { 
       // read the current frame 
       this.capture.read(frame); 

       // if the frame is not empty, process it 
       if (!frame.empty()) { 
        Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY); 
       } 

      } catch (Exception e) { 
       // log the error 
       System.err.println("Exception during the image elaboration: " + e); 
      } 
     } 

     return frame; 
    } 

    /** 
    * Stop the acquisition from the camera and release all the resources 
    */ 
    private void stopAcquisition() { 
     if (this.timer != null && !this.timer.isShutdown()) { 
      try { 
       // stop the timer 
       this.timer.shutdown(); 
       this.timer.awaitTermination(33, TimeUnit.MILLISECONDS); 
      } catch (InterruptedException e) { 
       // log any exception 
       System.err.println("Exception in stopping the frame capture, trying to release the camera now... " + e); 
      } 
     } 

     if (this.capture.isOpened()) { 
      // release the camera 
      this.capture.release(); 
     } 
    } 

    /** 
    * Update the {@link ImageView} in the JavaFX main thread 
    * 
    * @param view 
    *   the {@link ImageView} to update 
    * @param image 
    *   the {@link Image} to show 
    */ 
    private void updateImageView(ImageView view, Image image) { 

     Utils.onFXThread(view.imageProperty(), image); 
    } 

    /** 
    * On application close, stop the acquisition from the camera 
    */ 
    protected void setClosed() { 
     this.stopAcquisition(); 
    } 

    @FXML 
    protected void captureImg(ActionEvent event) { 
     if (this.cameraActive) { 
      if (num <= 5) { 
       t = Utils.mat2Image(frame); 
       temp = new ImageView(t); 
       temp.setFitWidth(120); 
       temp.setFitHeight(120); 
       for (int i = 0; i < imageList.size(); i++) { 
        if (imageList.get(i) != null) 
         continue; 
        imageList.set(i, temp); 
        break; // Exit loop this way only the most current pic is 
          // set. 
       } 

       imgHolder.getChildren().add(temp); 
       num++; 
      } else 
       System.out.println("max number of pics"); 
     } 

    } 

    @FXML 
    protected void clearAll(ActionEvent event) { 
     imgHolder.getChildren().clear(); 
     num = 0; // reset counter 
    } 

ここで保存処理が行われます。今、イメージは特定のファイルに保存されますが、別のコンピュータでイメージを実行できるようにしたいと考えています。そして

// 
    @FXML 
    protected void save(ActionEvent event) { 
     try { 
      File outputFile = new file("\\Desktop\\dipImages\\"); 
      for (int i =0 ; i < imageList.getSize(); i++) { 
      Image temp = imageList.get(i).getImage(); 

      BufferedImage bImage = SwingFXUtils.fromFXImage(temp, null); 
      try { 
       ImageIO.write(bImage, "jpg", outputFile + " " + i); 
      } catch (IOException) { 
       throw new RuntimeException(e); 

      } 

      } 

     } 

    } 

} 
+0

ですので、トップコードはまったく関係ありません。 –

+0

'javafx filechooser save file'のgoogleはどうですか? –

答えて

0

ちょうどファイルのFileChooserを使用し、保存する場所を選択:

FileChooser fileChooser = new FileChooser(); 
FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("JPEG image", "*.jpg"); 
fileChooser.getExtensionFilters().add(extensionFilter); 
fileChooser.setSelectedExtensionFilter(extensionFilter); 
File file = fileChooser.showSaveDialog(button.getScene().getWindow()); 
if (file != null) { 
    // save to file 
} 

またはDirectoryChooserは、ディレクトリを選択します。 Fileのコンストラクタを使用して、パラメータをbtwとして親のFileを使用する必要があります。

DirectoryChooser directoryChooser = new DirectoryChooser(); 
File directory = directoryChooser.showDialog(button.getScene().getWindow()); 
if (directory != null) { 
    for (int i = 0; i < imageList.getSize(); i++) { 
     File file = new File(directory, i+".jpg"); 
     // save image to file 
    } 
} 
関連する問題