我一直在尝试从JavaFX中的媒体播放器中拍摄快照,但是它一直在给我提供过饱和的图像。

从激发按钮获取的过饱和图像的样本。
Sample of the over saturated image taken from excited button.

我对如何制作普通快照一无所知。

任何帮助和建议,将不胜感激。先感谢您。 :)

这是我的Java代码:

public class FXMLDocumentController implements Initializable {
private MediaPlayer mediaPlayer;

@FXML
private MediaView mediaView;
private String filePath;

@FXML
private Slider seekSlider;

@FXML
private void handleButtonAction(ActionEvent event) {
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a File ('.mp4')", "*.mp4");
            fileChooser.getExtensionFilters().add(filter);
            File file = fileChooser.showOpenDialog(null);
            filePath = file.toURI().toString();

            if(filePath != null){
                Media media = new Media(filePath);
                mediaPlayer = new MediaPlayer(media);
                mediaView.setMediaPlayer(mediaPlayer);



        mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
        @Override
        public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
           seekSlider.setMin(0.0);
           seekSlider.setValue(0.0);
           seekSlider.setMax(mediaPlayer.getTotalDuration().toSeconds());
           seekSlider.setValue(newValue.toSeconds());
        }
            });

            seekSlider.setOnMouseClicked(new EventHandler<MouseEvent>(){
                    @Override
                    public void handle(MouseEvent event) {
                     mediaPlayer.seek(Duration.seconds(seekSlider.getValue()));
                    }

            });
     mediaPlayer.play();

            }

}
@FXML
private void pauseVideo(ActionEvent event){
     mediaPlayer.pause();
}
 @FXML
private void playVideo(ActionEvent event){
    mediaPlayer.play();
}
 @FXML
private void stopVideo(ActionEvent event){
    mediaPlayer.stop();
}
@FXML
private void exit(ActionEvent event){
    System.exit(0);
}

@FXML
private void excitedCapture(ActionEvent event) throws IOException{
   WritableImage img = mediaView.snapshot(new SnapshotParameters(), null);
   BufferedImage bufImg = SwingFXUtils.fromFXImage(img, null);
   ImageIO.write(bufImg, "jpg", new File("hi.jpg"));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

最佳答案

我认为过饱和不是这里的问题。我认为您只是被以下bug击中了:https://bugs.openjdk.java.net/browse/JDK-8119048

解决方法是,您可以将BufferedImage转换为不包含alpha的类型并将其写入。我测试了它,它对于PNG和JPEG格式都可以正常工作。

BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
BufferedImage bImage2 = new BufferedImage(bImage.getWidth(), bImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
bImage2.getGraphics().drawImage(bImage, 0, 0, null);
ImageIO.write(bImage2, FILE_TYPE, file);

10-08 12:42