javafx - 在 ImageView 中缩放图像 (javafx)

标签 javafx javafx-8 javafx-2 scenebuilder

我正在尝试使用 slider 缩放 ImageView 中的选定图像。我唯一能做的就是使用其中的图像调整整个 ImageView 的大小。但这不是我要寻找的。我只想缩放具有相同大小的 ImageView 的图像。应该是焦距的演示。我也在使用场景生成器。我只是所有这一切的初学者,所以我非常感谢你的帮助。这是我的。

public void openImage(ActionEvent event) {

    FileChooser fc = new FileChooser();
    File f = fc.showOpenDialog(null);
    if (f != null) {
        System.out.println(f.toURI().toString());
        Obrazok = new Image(f.toURI().toString());          
        imageView.setImage(Obrazok);
        imageView.setSmooth(true);
        imageView.setCache(true); 

    }
}
public void setOnZoom (MouseEvent e){

    imageView.scaleXProperty().bind(slider.valueProperty());
    imageView.scaleYProperty().bind(slider.valueProperty());
    Rectangle2D viewpoint = new Rectangle2D(0, 0, Obrazok.getRequestedHeight(), Obrazok.getRequestedWidth());
    imageView.setViewport(viewpoint);
    imageView.setViewport(viewpoint);
    imageView.setImage(Obrazok);
}

最佳答案

我不太确定你想做什么!我有点用了 James_D 的建议

好吧,我也是初学者,我花了将近两个小时为您写这篇文章,希望有人阅读并发现它有用

我只为 png 和 jpg 添加了 ExtensionFilters,但您可以添加 javaFx 支持的任何扩展

(我使用 slider 而不是滚动条,因为我讨厌处理滚动条,但我相信你可以自己做)

screenshot 01

screenshot 02

screenshot 03

screenshot 04

更新:现在您可以在 ImageView 上拖动以移动视口(viewport)

package application;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class Main extends Application {
    static double initx;
    static double inity;
    static int height;
    static int width;
    public static String path;
    static Scene initialScene,View;
    static double offSetX,offSetY,zoomlvl;
    @Override
    public void start(Stage s) {
        s.setResizable(false);
        GridPane grid = new GridPane();
        grid.setHgap(20);grid.setVgap(20);
        grid.setAlignment(Pos.CENTER);

        Label hint = new Label("Select Your Image");
        TextField URL = new TextField();
        URL.setEditable(false);
        URL.setPrefWidth(350);

        Button browse = new Button("Browse");
        FileChooser fc = new FileChooser();
        ExtensionFilter png = new ExtensionFilter("png", "*.png");
        ExtensionFilter jpg = new ExtensionFilter("jpg", "*.jpg");
        fc.getExtensionFilters().addAll(png,jpg);
        browse.setOnAction(e->{
            URL.setText(fc.showOpenDialog(s).getAbsolutePath());
        });

        Button open = new Button("Open");
        open.setOnAction(e->{
            path = URL.getText();
            initView();
            s.setScene(View);
        });

        grid.add(hint, 0, 0);
        grid.add(URL, 1, 0);
        grid.add(browse, 2, 0);
        grid.add(open, 2, 1);

        initialScene = new Scene(grid,600,100);
        s.setScene(initialScene);
        s.show();
    }

    public static void initView(){
        VBox root = new VBox(20);
        root.setAlignment(Pos.CENTER);

        Label title = new Label(path.substring(path.lastIndexOf("\\")+1));
        Image source = null;
        try {
            source = new Image(new FileInputStream(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ImageView image = new ImageView(source);
        double ratio = source.getWidth()/source.getHeight();

        if(500/ratio < 500) {
            width=500;
            height=(int) (500/ratio);
        }else if(500*ratio < 500){
            height=500;
            width=(int) (500*ratio);
        }else {
            height=500;
            width=500;
        }
        image.setPreserveRatio(false);
        image.setFitWidth(width);
        image.setFitHeight(height);
        height = (int) source.getHeight();
        width = (int) source.getWidth();
        System.out.println("height = "+height+"\nwidth = "+width);
        HBox zoom = new HBox(10);
        zoom.setAlignment(Pos.CENTER);

        Slider zoomLvl = new Slider();
        zoomLvl.setMax(4);
        zoomLvl.setMin(1);
        zoomLvl.setMaxWidth(200);
        zoomLvl.setMinWidth(200);
        Label hint = new Label("Zoom Level");
        Label value = new Label("1.0");

        offSetX = width/2;
        offSetY = height/2;



        zoom.getChildren().addAll(hint,zoomLvl,value);

        Slider Hscroll = new Slider();
        Hscroll.setMin(0);
        Hscroll.setMax(width);
        Hscroll.setMaxWidth(image.getFitWidth());
        Hscroll.setMinWidth(image.getFitWidth());
        Hscroll.setTranslateY(-20);
        Slider Vscroll = new Slider();
        Vscroll.setMin(0);
        Vscroll.setMax(height);
        Vscroll.setMaxHeight(image.getFitHeight());
        Vscroll.setMinHeight(image.getFitHeight());
        Vscroll.setOrientation(Orientation.VERTICAL);
        Vscroll.setTranslateX(-20);


        BorderPane imageView = new BorderPane();
        BorderPane.setAlignment(Hscroll, Pos.CENTER);
        BorderPane.setAlignment(Vscroll, Pos.CENTER_LEFT);
        Hscroll.valueProperty().addListener(e->{
            offSetX = Hscroll.getValue();
            zoomlvl = zoomLvl.getValue();
            double newValue = (double)((int)(zoomlvl*10))/10;
            value.setText(newValue+"");
            if(offSetX<(width/newValue)/2) {
                offSetX = (width/newValue)/2;
            }
            if(offSetX>width-((width/newValue)/2)) {
                offSetX = width-((width/newValue)/2);
            }

            image.setViewport(new Rectangle2D(offSetX-((width/newValue)/2), offSetY-((height/newValue)/2), width/newValue, height/newValue));
        });
        Vscroll.valueProperty().addListener(e->{
            offSetY = height-Vscroll.getValue();
            zoomlvl = zoomLvl.getValue();
            double newValue = (double)((int)(zoomlvl*10))/10;
            value.setText(newValue+"");
            if(offSetY<(height/newValue)/2) {
                offSetY = (height/newValue)/2;
            }
            if(offSetY>height-((height/newValue)/2)) {
                offSetY = height-((height/newValue)/2);
            }
            image.setViewport(new Rectangle2D(offSetX-((width/newValue)/2), offSetY-((height/newValue)/2), width/newValue, height/newValue));
        });
        imageView.setCenter(image);
        imageView.setTop(Hscroll);
        imageView.setRight(Vscroll);
        zoomLvl.valueProperty().addListener(e->{
            zoomlvl = zoomLvl.getValue();
            double newValue = (double)((int)(zoomlvl*10))/10;
            value.setText(newValue+"");
            if(offSetX<(width/newValue)/2) {
                offSetX = (width/newValue)/2;
            }
            if(offSetX>width-((width/newValue)/2)) {
                offSetX = width-((width/newValue)/2);
            }
            if(offSetY<(height/newValue)/2) {
                offSetY = (height/newValue)/2;
            }
            if(offSetY>height-((height/newValue)/2)) {
                offSetY = height-((height/newValue)/2);
            }
            Hscroll.setValue(offSetX);
            Vscroll.setValue(height-offSetY);
            image.setViewport(new Rectangle2D(offSetX-((width/newValue)/2), offSetY-((height/newValue)/2), width/newValue, height/newValue));
        });
        imageView.setCursor(Cursor.OPEN_HAND);
        image.setOnMousePressed(e->{
            initx = e.getSceneX();
            inity = e.getSceneY();
            imageView.setCursor(Cursor.CLOSED_HAND);
        });
        image.setOnMouseReleased(e->{
            imageView.setCursor(Cursor.OPEN_HAND);
        });
        image.setOnMouseDragged(e->{
            Hscroll.setValue(Hscroll.getValue()+(initx - e.getSceneX()));
            Vscroll.setValue(Vscroll.getValue()-(inity - e.getSceneY()));
            initx = e.getSceneX();
            inity = e.getSceneY();
        });
        root.getChildren().addAll(title,imageView,zoom);

        View = new Scene(root,(image.getFitWidth())+70,(image.getFitHeight())+150);
    }

    public static void main(String[] args) {
        launch(args);
    }
}    

关于javafx - 在 ImageView 中缩放图像 (javafx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48687994/

相关文章:

java - 如何在 ControlsFX (JavaFX 8) 中使用 BreadCrumbBar

java - 以编程方式编辑 TreeView/TreeItem

JavaFX - 如何使用来自另一个 Controller 的 Controller 中的方法?

javafx-2 - JavaFX 折线图中的空指针异常

java - 从 java 代码启动 javaFX 应用程序

logging - 转换为 UI 的记录器条目停止随时间更新

javascript - 一次显示一个元素

JavaFX 在 ImageView 中显示像素的 RGB 值

java - 使用多线程在一定时间内更改javafx圆圈颜色

JavaFX 动画恢复到原始状态