Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javafx从mysql接收图像集到图像视图_Java_Javafx_Javafx 2_Javafx 8 - Fatal编程技术网

javafx从mysql接收图像集到图像视图

javafx从mysql接收图像集到图像视图,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我的mysql数据库中有图像,我想接收图像,我可以用java实现,但不能用javafx 我使用此代码从数据库接收图像 byte[] f51 = rs.getBytes(11); 现在我想在图像视图中设置它 我可以在Java中这样做,比如img.setIcon(newimageicon(f51))但在javafx中不起作用 我用了密码 private static Image convertToJavaFXImage(byte[] raw, final int width, final int

我的mysql数据库中有图像,我想接收图像,我可以用java实现,但不能用javafx

我使用此代码从数据库接收图像

 byte[] f51 = rs.getBytes(11);
现在我想在
图像视图中设置它

我可以在Java中这样做,比如
img.setIcon(newimageicon(f51))但在javafx中不起作用

我用了密码

private static Image convertToJavaFXImage(byte[] raw, final int width, final int height) {
    WritableImage image = new WritableImage(width, height);
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(raw);
        BufferedImage read = ImageIO.read(bis);
        SwingFXUtils.toFXImage(read, image);
    } catch (IOException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
    return image;
}
由@Patrick提供

现在它给出了错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
    at com.sun.glass.ui.View.notifyMouse(View.java:925)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/584634336.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
    ... 30 more
Caused by: java.lang.NullPointerException
    at javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85)
    at stockmanagement1.RawmaterialAddController.convertToJavaFXImage(RawmaterialAddController.java:340)
    at stockmanagement1.RawmaterialAddController.tabmrel(RawmaterialAddController.java:318)
    ... 40 more
请帮帮我

已更新

等等,我想有一个困惑,我已经发布了所有代码:

添加:

     @FXML
        private void add(ActionEvent event) {

                      String mcode=txt1.getText();
                   String mname=txt2.getText();
                   String material=txt3.getText();
                   String quantity=txt4.getText();
                   String unit=(String) com1.getSelectionModel().getSelectedItem();
                   String priceper=txt5.getText();
                   String godown=(String) com2.getSelectionModel().getSelectedItem();

                   String desc=txtdexc.getText();

                byte[] buf = null; byte[] person_image5=null;
           try{
        String df = img.getText();
    File image=new File (df);
    FileInputStream fis=new FileInputStream(image);
    ByteArrayOutputStream bos= new ByteArrayOutputStream();
     buf=new byte[1024];
    for(int readnum;(readnum=fis.read(buf))!=-1; ){
    bos.write(buf,0,readnum);
}
 person_image5 = bos.toByteArray();
}
   catch(Exception e){ }   
       try{
            Connection con1=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/stockmanagement","root","root");
            String sql1="INSERT INTO `rawmaterial`(`Name`, `Code`, `DOC`, `Material`, `godown`, `quantity`, `unit`, `cost`, `detail`, `pic`, `user`, `lastupdated`)"
                    + "                 VALUES    ('"+mname+"','"+mcode+"','2012-12-12','"+material+"','"+godown+"','"+quantity+"','"+unit+"','"+priceper+"','"+desc+"','"+person_image5+"','','')";
            PreparedStatement stm1=(PreparedStatement) con1.prepareStatement(sql1);
            stm1.executeUpdate(sql1);   
            tabdata();
                 } 

                 catch(Exception e){
                     System.out.println(e.getMessage());
 }

    }
现在是显示图像的代码

        @FXML
    void tabmrel(MouseEvent event) throws IOException {
    int x0=table.getSelectionModel().getSelectedIndex();
      String id=table.getItems().get(x0).getFirstName5();
              try{
    Class.forName("com.mysql.jdbc.Driver");
        Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/stockmanagement","root","root");
        String sql="Select * from rawmaterial where code='"+id+"'";
       Statement stm=(Statement) con.createStatement();
      ResultSet rs=stm.executeQuery(sql);

       if(rs.next()){
    txt1.setText(rs.getString(3));
     txt2.setText(rs.getString(2));
   txt3.setText(rs.getString(5));
    txt4.setText(rs.getString(7));
     byte[]  f51=rs.getBytes(11); 
          System.out.println(f51);

            ByteArrayInputStream bis = new ByteArrayInputStream(f51);
            System.out.println(bis);
            BufferedImage read = ImageIO.read(bis);
            System.out.println(read);
            Image image = SwingFXUtils.toFXImage(read, null);
            imagego.setImage(image);

      com1.getSelectionModel().select(rs.getString(8));
              txt5.setText(rs.getString(9));
           com1.getSelectionModel().select(rs.getString(6));
       txtdexc.setText(rs.getString(10));

     }
     } 
    catch (ClassNotFoundException | SQLException e) {

    }
  }
帮帮我。

嘿,用户3829658

您可以尝试以下方法:

private static Image convertToJavaFXImage(byte[] raw, final int width, final int height) {
        WritableImage image = new WritableImage(width, height);
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(raw);
            BufferedImage read = ImageIO.read(bis);
            image = SwingFXUtils.toFXImage(read, null);
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
        return image;
    }
更新 一个完整的工作示例,唯一的区别是,我从SSD加载映像,并从
Files.readAllBytes()方法获取
字节[]

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javax.imageio.ImageIO;


public class App extends Application {

    @Override
    public void start(Stage primaryStage) throws FileNotFoundException, IOException {

        AnchorPane root = new AnchorPane();

        // replace this PATH_TO_YOUR_IMAGE
        Path get = Paths.get("<PATH_TO_YOUR_IMAGE>");
        byte[] readAllBytes = Files.readAllBytes(get);
        Image convertToJavaFXImage = convertToJavaFXImage(readAllBytes, 1024, 768);
        ImageView view = new ImageView(convertToJavaFXImage);
        root.getChildren().add(view);
        Scene scene = new Scene(root, 1024, 768);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private static Image convertToJavaFXImage(byte[] raw, final int width, final int height) {
        WritableImage image = new WritableImage(width, height);
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(raw);
            BufferedImage read = ImageIO.read(bis);
            image = SwingFXUtils.toFXImage(read, null);
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
        return image;
    }

}
导入java.awt.image.buffereImage;
导入java.io.ByteArrayInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javafx.application.application;
导入javafx.embed.swing.SwingFXUtils;
导入javafx.scene.scene;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.scene.image.WritableImage;
导入javafx.scene.layout.ancorpane;
导入javafx.stage.stage;
导入javax.imageio.imageio;
公共类应用程序扩展应用程序{
@凌驾
public void start(Stage primaryStage)抛出FileNotFoundException、IOException{
锚烷根=新锚烷();
//将此路径替换为您的图像
Path get=路径。get(“”);
byte[]readAllBytes=Files.readAllBytes(get);
Image convertToJavaFXImage=convertToJavaFXImage(readAllBytes,1024768);
ImageView=新的ImageView(convertToJavaFXImage);
root.getChildren().add(视图);
场景=新场景(根,1024768);
setTitle(“你好,世界!”);
初级阶段。场景(场景);
primaryStage.show();
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
发射(args);
}
私有静态图像convertToJavaFXImage(字节[]原始,最终整型宽度,最终整型高度){
WritableImage image=新的WritableImage(宽度、高度);
试一试{
ByteArrayInputStream bis=新的ByteArrayInputStream(原始);
BuffereImage读取=图像IO.read(bis);
image=SwingFXUtils.toFXImage(读取,空);
}捕获(IOEX异常){
Logger.getLogger(App.class.getName()).log(Level.SEVERE,null,ex);
}
返回图像;
}
}

帕特里克

对我们其他人来说

有一个更简单的方法来做这件事


使用图像填充ImageView对象后,您可以根据自己的需要设置其属性和方法。

它在线程“JavaFX应用程序线程”java.lang.RuntimeException中给出错误
异常:java.lang.reflect.InvocationTargetException位于JavaFX.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)30多个原因:StockManagements 1.RawmaterialAddController.convertToJavaFXImage(RawmaterialAddController.java:340)的javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85)位于StockManagements 1.RawmaterialAddController.tabmrel的java:340(RawmaterialAddController.java:318)40更多
在RawmaterialAddController类的第340行或第318行,存在一个NullPointerException。您还可以添加此控制器类吗?行是
Image sds=convertToJavaFXImage(F51200200);
-318,其中f51是byte`byte[]f51=rs.getBytes(11)5944
            String SQL = "SELECT File from SomeTable WHERE ID = ?";
            PreparedStatement pstmt = conn.prepareStatement(SQL);
            pstmt.setInt(1, fileID);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                InputStream imageFile = rs.getBinaryStream(1);
                Image image = new Image(imageFile);
                ImageView iv = new ImageView(image);
            }