java 在 Android 上将文本转换为图像文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9973048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:07:03  来源:igfitidea点击:

Convert text to image file on Android

javaandroidfile-format

提问by Seshu Vinay

I have a text document (.txt). I want to convert it to an image (.png or .jpg). For example, black text on white background. How can I do that programmatically?

我有一个文本文档 (.txt)。我想将其转换为图像(.png 或 .jpg)。例如,白底黑字。我怎样才能以编程方式做到这一点?

回答by M-WaJeEh

I think the proper way for multi-line text is this:

我认为多行文本的正确方法是这样的:

String text = "This \nis \nmultiline";

final Rect bounds = new Rect();
TextPaint textPaint = new TextPaint() {
    {
        setColor(Color.WHITE);
        setTextAlign(Paint.Align.LEFT);
        setTextSize(20f);
        setAntiAlias(true);
    }
};
textPaint.getTextBounds(text, 0, text.length(), bounds);
StaticLayout mTextLayout = new StaticLayout(text, textPaint,
            bounds.width(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int maxWidth = -1;
for (int i = 0; i < mTextLayout.getLineCount(); i++) {
    if (maxWidth < mTextLayout.getLineWidth(i)) {
        maxWidth = (int) mTextLayout.getLineWidth(i);
    }
}
final Bitmap bmp = Bitmap.createBitmap(maxWidth , mTextLayout.getHeight(),
            Bitmap.Config.ARGB_8888);
bmp.eraseColor(Color.BLACK);// just adding black background
final Canvas canvas = new Canvas(bmp);
mTextLayout.draw(canvas);
FileOutputStream stream = new FileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();

回答by Renard

this (untested) code should get you on the right track.

这个(未经测试的)代码应该让你走上正轨。

void foo(final String text) throws IOException{
    final Paint textPaint = new Paint() {
        {
            setColor(Color.WHITE);
            setTextAlign(Paint.Align.LEFT);
            setTextSize(20f);
            setAntiAlias(true);
        }
    };
    final Rect bounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), bounds);

    final Bitmap bmp = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.RGB_565); //use ARGB_8888 for better quality
    final Canvas canvas = new Canvas(bmp);
    canvas.drawText(text, 0, 20f, textPaint);
    FileOutputStream stream = new FileOutputStream(...); //create your FileOutputStream here
    bmp.compress(CompressFormat.PNG, 85, stream);
    bmp.recycle();
    stream.close();
}

回答by guness

This is what you need:

这是你需要的:

http://mvnrepository.com/artifact/org.apache.xmlgraphics/xmlgraphics-commons/1.3.1

http://mvnrepository.com/artifact/org.apache.xmlgraphics/xmlgraphics-commons/1.3.1

I can provide you sample code if you want.

如果您愿意,我可以为您提供示例代码。

Edit: simple example: package v13;

编辑:简单示例:包 v13;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.xmlgraphics.image.codec.png.PNGImageEncoder;

public class Deneme {
    public static void main(String[]args){
        JFrame jf = new JFrame();
        jf.setVisible(true);

        JPanel jp = new JPanel();
        jf.add(jp); 
        JLabel jl = new JLabel("trial text");
        jf.add(jl);
        jf.setSize(300, 200);

        JFileChooser jfc = new JFileChooser();
        int temp = jfc.showSaveDialog(jfc);
        if (temp == JFileChooser.APPROVE_OPTION) {
            System.out.println(jfc.getSelectedFile());
            Component myComponent = jf;
            Dimension size = myComponent.getSize();
            BufferedImage myImage = new BufferedImage(size.width,
                    size.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = myImage.createGraphics();
            myComponent.paint(g2);
            try {
                OutputStream out = new FileOutputStream(jfc
                        .getSelectedFile().getAbsolutePath()
                        + ".png");
                PNGImageEncoder encoder = new PNGImageEncoder(out, null);
                encoder.encode(myImage);
                out.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
       }
    }