gpt4 book ai didi

安卓。如何在真正的大图像(位图)上写文字并保存

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:10 24 4
gpt4 key购买 nike

您好!

是以下问题:我需要在相当大的图像上写一段长文本并将其保存在那里。

条件:

  1. 图片尺寸超过 3000 x 2000;
  2. 不允许将图像分成几部分,因为文本要从边到边写在整个图像上。
  3. 补充:我不能使用位图比例或矩阵比例,因为必须在原始图像(全尺寸)上书写文本。
  4. 添加:当我向用户显示图像时,我使用位图缩放:options.inSampleSize = 10; 没有内存不足的问题

目前的问题:

当我尝试在位图中加载此图像时,由于内存(VM 预算)的限制而发生错误。

问题:

  1. 如何在不加载到内存的情况下在大图像上输入文字?
  2. 现有的图书馆可以吗?

代码示例:

// Step 1. Open image and load to Bitmap
Bitmap bitmap = BitmapFactory.decodeFile(fileName); // runtime Exception like "VM budget" !
// or
//Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.big_img_1); // as in previous runtime Exception like "VM budget" !
// or
//Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream); // as in previous runtime Exception like "VM budget" !


// Step 2. Write text
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(14);

Canvas canvas = new Canvas(bitmap);
canvas.drawText("some long long string over all image", 100, 100, paint);

// Step 3. Save bitmap to file
OutputStream fOut = new FileOutputStream(new File(fileName));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
/*
_________________________________________________
| |
| |
| |
| |
| IMG |
| |
| |
| some long long string over all image |
|________________________________________________|
*/

最佳答案

首先使用下面的帮助加载大位图

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

其次要在图片上写字并保存,可以用canvas

我在一个应用程序中做了类似的事情。使用 Canvas 。

我编辑了一段代码,它实际上在背景上添加了一些其他图像等等。

代码内容:

private static Bitmap getPoster(...) {
Bitmap background = BitmapFactory.decodeResource(res, background_id)
.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(background);
Typeface font = Typeface.createFromAsset(res.getAssets(), FONT_PATH);
font = Typeface.create(font, Typeface.BOLD);
Paint paint = new Paint();
paint.setTypeface(font);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
float fontSize = getFontSize(background.getWidth(), THE_QUOTE, paint); //You'll have to define a way to find a size that fits, or just use a constant size.

paint.setTextSize(fontSize);
canvas.drawText(THE_QUOTE, (background.getWidth() - paint.measureText(THE_QUOTE)) / 2,
background.getHeight() - FILLER_HEIGHT, paint); //You might want to do something different. In my case every image has a filler in the bottom which is 50px.
return background;
}

将您自己的版本放入一个类中,并为其提供图像 ID 和其他任何内容。它返回一个位图供您做任何您想做的事情(在 ImageView 中显示它,让用户保存它并设置为墙纸)。

关于安卓。如何在真正的大图像(位图)上写文字并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17292868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com