【问题标题】:Getting the "native size" of an image created with JLabel获取使用 JLabel 创建的图像的“原始大小”
【发布时间】:2012-02-17 01:27:03
【问题描述】:

I am using JLabel 从字符串创建图像文件。

我必须指定图像尺寸 (label.setSize(width, height)),否则会出现异常:

java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:338)
    at com.shopsnips.portal.services.ImageCreator.createFromText(ImageCreator.java:31)
    at com.shopsnips.portal.services.ImageCreator.main(ImageCreator.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我可以使用控制字体大小

label.setFont(new Font("Serif", Font.BOLD, 26));

当我使用太大而无法容纳固定尺寸的字体或文本时,标签会被截断,而包含“...”。有没有办法确定仍然适合我设置的尺寸的最佳/最大字体大小?

或者,如何确定当前设置(字体大小+尺寸)是否会导致文本被截断?

这里有一些来源:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ImageCreator {
    private ImageCreator(){}

    private final static String FONT = "Freestyle Script";

    public static void main(String[] args) {
        Path outputFile = Paths.get("c:\\tmp\\img\\test.png");

        createFromText("Hello World - this is a long text", outputFile, 150, 50);
    }

    /**
* <p>Create an image from text. <p/>
* <p/>
* https://stackoverflow.com/a/4437998/11236
*/
    public static void createFromText(String text, Path outputFile, int width, int height) {
        JLabel label = new JLabel(text, SwingConstants.CENTER);
        label.setSize(width, height);
        label.setFont(new Font(FONT, Font.BOLD, 24));

        BufferedImage image = new BufferedImage(
                label.getWidth(), label.getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        Graphics g = null;
        try {
            // paint the html to an image
            g = image.getGraphics();
            g.setColor(Color.BLACK);
            label.paint(g);
        } finally {
            if (g != null) {
                g.dispose();
            }
        }

        // get the byte array of the image (as jpeg)
        try {
            ImageIO.write(image, "png", outputFile.toFile());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

请登录后发表评论。

【问题讨论】:

    标签: java image swing jlabel


    【解决方案1】:

    1) 将BuferedImage 作为Icon 放到JLabel

    2) 不要setSize 让这份工作为LayoutManager 工作

    3) @Jeffrey 的回答与正确答案太接近,如果存在 BufferedImage 可以返回两个维度

    4) 为了获得更好的帮助,请尽快发布SSCCE,因为我/我们在您的显示器上看不到代码,也看不到您的 Java 类生成的异常

    【讨论】:

    • 我正在使用我链接到的帖子......它几乎是一个 SSCCE。我可以发布我自己的代码,它非常非常接近。只需省略设置尺寸即可。
    • 还有……这是您的 SSCCE。明天上班时我会试试你的答案。 gist.github.com/1665287
    • 1) “是 SSCCE”或“不是不是 SSCCE”,“接近”没有奖品。 2) 人们通常不会关注 github 等外部网站的链接。最好将源代码作为编辑嵌入到问题中。我已经做到了。
    • @ripper234 Swing GUI 的所有更新都必须在 EDT 上完成,然后您必须将后台任务的输出包装到 invokeLater / invokeAndWait(有时依赖)更多 docs.oracle.com/javase/tutorial/uiswing/concurrency/…
    • 这里有些答案/cmets 不同意你的观点:stackoverflow.com/questions/8975533/…
    【解决方案2】:

    label.setFont(new Font("Serif", Font.BOLD, 26)); ..有没有办法确定仍然适合我设置的尺寸的最佳/最大字体大小?

    要获取文本大小,请查看FontMetricsGlyphVector

    获取文本大小的“快速而肮脏”的方法是将其放入标签中并询问标签以获得首选大小。

    根据这些数字,字体大小可以做相应的调整。

    【讨论】:

      【解决方案3】:

      我不喜欢给出的任何答案(或者没有足够详细的信息供我现在使用)。

      相反,我只是使用这种启发式方法来选择字体大小:

      private static int chooseFontSize(String text) {
          int largeFont = 28;
          int mediumFont = 22;
          int tinyFont = 16;
          if (text.length() > 25) {
              return tinyFont;
          }
          if (text.length() > 15) {
              return mediumFont;
          }
          return largeFont;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-28
        • 2012-12-04
        • 2012-01-09
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 2012-03-16
        • 1970-01-01
        相关资源
        最近更新 更多