使用绘画增强文本质量

时间:2017-04-17 20:26:51

标签: android text android-bitmap itextg

我需要生成印地语PDF并且我正在使用iTextg来实现此目的。 To"输入"在印地语中,我使用Android的静态布局将文本转换为位图。但是我的文字质量很差。

我的代码如下

public Bitmap textAsBitmap(String text, float textSize, float stroke, int color, int align) {
//All these flags were added after desperate attempts
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.HINTING_ON | Paint.LINEAR_TEXT_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    paint.setTextSize(textSize);
    paint.setColor(color);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(stroke);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
    float baseline = (int) (-paint.ascent() + 3f); // ascent() is negative
    Layout.Alignment mAlignment;
    if (align == Element.ALIGN_CENTER)
        mAlignment = Layout.Alignment.ALIGN_CENTER;
    else if (align == Element.ALIGN_RIGHT)
        mAlignment = Layout.Alignment.ALIGN_OPPOSITE;
    else mAlignment = android.text.Layout.Alignment.ALIGN_NORMAL;

    StaticLayout staticLayout = new StaticLayout(text, 0, text.length(),
            paint, 435, mAlignment, 1.0f, 1.0f, false);

    Bitmap image = Bitmap.createBitmap(staticLayout.getWidth(),
            staticLayout.getHeight() + 2, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);
    canvas.drawColor(Color.WHITE);

    canvas.drawBitmap(image, 5, 5, paint);

    staticLayout.draw(canvas);
    return image;
}

结果 Result

帮助我,因为稍后需要打印这些PDF文件。

0 个答案:

没有答案