8

我想将编码图像嵌入到 FXML 中(我需要这个,因为我正在编写 svg 和 fxml 之间的转换器)。我试过这个:

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
    <children>
        <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="153.0" layoutY="94.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYEAYAAACw5+G7AAAABmJLR0T///////8JWPfcAAAA CXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAYAAAAGAB4TKWmAAAI1UlEQVRYw61YS4gc VRf+7r1Vt+pWdfdMd0/P9PTMOCGJBpNI4gPiRrPQnSKIigQEN4LgQoTgwpU7QYOggop7UVA3 ESMEDKiJmIlEhUwS85pM5tGP6Wemn/V2cVJ2xxjCr//d3K66davOd873nXNus+jGwH8cjuM4 jgN0Op1OpwOEYRiGIcAY54wBQgihaUAqlUwmkwDnnEcRwBhjmvbvv8v+VwCO43muC1y6dOHC 0hJQKhWL5TIghKYJARQK09PpNDA1lc9nMoBlWVYyCdRqtdrmJrC+vr5eqQAAYwAwOzs3VygA 09NTU1NTBIhW/k8AoigMgwBYWbl2rVgEfvnl998XF4F6vVq9dg2YmUkmGQNyOSn7fYBzxlx3 uF8IIYQANE3XlaI7ySRQrfq+YQBra9Vqrwdks7nczAzw6KOPPPLww0AymUwmEncGdFsAnud5 vg9cvbq8fPUq0GiUSpUK0GqVSouLgBC9XqkEWBbn3S6g62HougDnUaTrIx9gjHE+vA4C8r3r apquA5ubvp9IAL0eY9kskMnMzc3PA/feu3v3nj0UmXz+9gD+Yl8URVEQEHejCKhUyuVKBbBt KcMQaDY3N5eWgPHxKNrYIGo4Du2UEghDQCkgioRgDBg1enTE9w2DZtsG+n2g23Wc1VVASt9P JoFOp93u9YBqVdMaDWBiIpNJp2+NyC3y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/l5YM+ehx568MEhgDuNf32ov3jx0qWrV4FisVQa bSFmZ6em0mlgbq5QKBTI84kEUC4Xi6USsLFRrzebQ63Nzs7MFApAPj85mc1SZb7T2eI/Afj7 iHf7vu8HAdDtUr/vODeaLY1zzgHTVMo0qXeiyFCd+K9/q/wJwRXXOxs1rPEAAAAASUVORK5C YII= " preserveRatio="false" smooth="false" />
            </image>
        </ImageView>
    </children>
</AnchorPane>

如您所见,我使用了对 HTML 或 CSS 执行的标准方式,通过图像的 Base64 编码传递数据 url。在这种情况下,它不会返回任何错误,但不会显示图像(将 url 传递给与文件相同的图像有效,但这不是我想要的)。

有人可以帮助我吗?

非常感谢!

4

1 回答 1

10

我能够做到这一点,但不是在 FXML

String rocketImgStr = "iVBORw0KGgoAAAANSUhEUgAAADIAAAAdCAYAAADoxT9SAAAACXBIWXMAAAsYAAALGAGJqbUQAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgABeNMLCADATZvAMByH/w/qQplcAYCEAcB0kThLCIAUAEB6jkKmAEBGAYCdmCZTAKAEAGDLY2LjAFAtAGAnf+bTAICd+Jl7AQBblCEVAaCRACATZYhEAGg7AKzPVopFAFgwABRmS8Q5ANgtADBJV2ZIALC3AMDOEAuyAAgMADBRiIUpAAR7AGDIIyN4AISZABRG8lc88SuuEOcqAAB4mbI8uSQ5RYFbCC1xB1dXLh4ozkkXKxQ2YQJhmkAuwnmZGTKBNA/g88wAAKCRFRHgg/P9eM4Ors7ONo62Dl8t6r8G/yJiYuP+5c+rcEAAAOF0ftH+LC+zGoA7BoBt/qIl7gRoXgugdfeLZrIPQLUAoOnaV/Nw+H48PEWhkLn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FjVAysjkchtNy5Z0nDd4sXuSCRCSSCA7DiYeTCjJc1ocJIkFYKXZRlFUcY8jwIdLfVTqRQdnZ38at269CddXW+nYTOw60rdEF9eJUk/9NfWojU0MHPBAmbX1VE1fTo+nw+Xy1WQn+M4E4CNgnAch2w2Szwep6enh48++shsb2/f393d/RfgT3l358rd2efBlpegzgaOAgeAFrf7pF5Zua+6qmpmKBQqr6io0MvKyhS/34/H40FRFGzbLtxPTp06Zff39yd7e3ujvb29B2Ox2F7gQ2BfPjldORflKrh5Fdz0GGiVQDy/yf4M/AieMUdclKnAVee4KMVAUT6DOvlD+QwwCAzkXZQk/2M7r6+lQIUONVOhugZqZkPoenBfD1SOm+B1YA109H/JvtYYp3EmiCYQm0F8COIzELlxTmMCxGEQr4JohKzvK+Q0TvB+AxAphppiKC+CYm2kqCSb934H4PjAiOf7lfF+/zMAVaPsnAfVjSoAAAAASUVORK5CYII=";
BASE64Decoder base64Decoder = new BASE64Decoder();
ByteArrayInputStream rocketInputStream = new ByteArrayInputStream(base64Decoder.decodeBuffer(rocketImgStr));

Image rocketImg = new Image(rocketInputStream);

希望有帮助。

于 2012-08-17T05:18:29.387 回答