IT干货网

java之JavaFX 中的快照过度饱和

sanshao 2024年06月03日 编程设计 36 0

我一直在尝试从 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 
}     

请您参考如下方法:

我不认为过度饱和是这里的问题。我认为您刚刚被这个错误击中: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); 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!