javafx开发- ImageView标签设置图片不显示

古城微笑少年丶 2023-10-10 22:46 106阅读 0赞

图片不显示的原因没找到, 但是找到了解决办法 嘿嘿…

解决问题代码如下:

  1. <ImageView fx:id="yj_img" fitHeight="16.0" fitWidth="16.0" layoutX="573.0" layoutY="8.0" pickOnBounds="true" preserveRatio="true">
  2. <image>
  3. <Image url="@../image/yj_green.png" />
  4. </image>
  5. </ImageView>
  6. /**
  7. * @Title: GetImageStrFromPath
  8. * @Description: TODO(将一张本地图片转化成Base64字符串)
  9. * @param imgPath
  10. * @return
  11. */
  12. public static String GetImageStrFromPath(String imgPath) {
  13. InputStream in = null;
  14. byte[] data = null;
  15. // 读取图片字节数组
  16. try {
  17. in = new FileInputStream(imgPath);
  18. data = new byte[in.available()];
  19. in.read(data);
  20. in.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. // 对字节数组Base64编码
  25. BASE64Encoder encoder = new BASE64Encoder();
  26. // 返回Base64编码过的字节数组字符串, 在win环境中会自动换行, 因此需要把换行符给去掉
  27. return encoder.encode(data).replaceAll("\r\n","");
  28. }
  29. @FXML
  30. public ImageView yj_img;
  31. @SneakyThrows
  32. @Override
  33. public void initialize(URL location, ResourceBundle resources) {
  34. /** 这段的功能是实时时间的显示 */
  35. //设置实时时间
  36. SimpleDateFormat currentTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); //设置时间格式
  37. EventHandler<ActionEvent> eventHandler = e->{
  38. sj_La.setText(currentTime.format(new Date()));
  39. // System.out.println(currentTime.format(new Date()));
  40. };
  41. //一秒刷新一次
  42. Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), (javafx.event.EventHandler<ActionEvent>) eventHandler));
  43. animation.setCycleCount(Timeline.INDEFINITE);
  44. animation.play();
  45. /** 这段的功能是实时时间的显示 */
  46. /**
  47. * 将图片转为base64字符串,然后放到ImageView标签中就可以显示了
  48. */
  49. URL resource = getClass().getResource("/image/yj_gray.png");
  50. String imgPath = resource.toURI().getPath().trim();
  51. // System.out.println(imgPath);
  52. String base64Str = Base64ImgUtil.GetImageStrFromPath(imgPath);
  53. // System.out.println("64格式:--" + base64Str + "---" + base64Str.length());
  54. // String base64String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAYVJREFUOE/Nk89LAlEQx2d2N3+EYqduQZeOEUFRCUUrbkS6KlR2ic5dvUbdqmuXDh06RgczUFcjWnPDwrpFdOw/6JQlKLr7JjQVF/IHdGlOj5nv+/CdN28Q/hjY7X48qUZq9VBAOuqk6w5QMtE6QPaGewLiaTXCMz4ry56Xpjh5pc7WzoEV6amZU5TshMEZnpDvx1XdgaZpQqGoXwPBuEWojulk9RHRNhFM1kUIzwzZiRX1dEUfeAOEV5dDWBZFUW+1EM3n7baP0iIz2CYAbCBClDG8RaTTNvvnHM+dlYfsd2G3u9Ry0BQkFHWXAHc4MvxOpyX3WarOkIEP7f0jwF5QlvabuZYDTdNshaL+DhwcVgaFY2tRzxLA9C+P9+VyCMOiKJZNDuKpzDwQ5Sy8MFJh1S0gPOg4YsSFkN97bwIk0+oqYxADwBggTAHRaCcAx8FawCddmgCJ1M06Edbn3isQKRz0L138M0BcUfMAMNfLfqP+GJIlt3kKjX/fJ6C1H12XqR/YN1DkmhHCB7aNAAAAAElFTkSuQmCC";
  55. // System.out.println(base64String.length());
  56. byte[] imageBytes = Base64.getDecoder().decode(base64Str);
  57. InputStream inputStream = new ByteArrayInputStream(imageBytes);
  58. DataInputStream dataInputStream = new DataInputStream(inputStream);
  59. yj_img.setImage(new Image(dataInputStream));
  60. /**
  61. * 状态!=就绪, 按钮不可点击
  62. */
  63. startTest_btu.setDisable(true);
  64. }

发表评论

表情:
评论列表 (有 0 条评论,106人围观)

还没有评论,来说两句吧...

相关阅读