Android 在图像上写入文本而不增加大小?
我正在图像上写一些文字,这些文字将被压缩 图像sieAndroid 在图像上写入文本而不增加大小?,android,optimization,android-canvas,bitmapimage,image-compression,Android,Optimization,Android Canvas,Bitmapimage,Image Compression,我正在图像上写一些文字,这些文字将被压缩 图像sie2048x1536px 我第一次试着先写 它抛出了OutOfMemory异常 然后我首先将其压缩为1024x768 然后在图像上写入文本 它将图像的KB从100KB增加到640KB 在编写文本时,我可以降低图像质量,但不能降低文本质量 当压缩质量设置为30时,文本也会进入downsample 问题: 是否有先写后压缩或先压缩后写 文本而不更改图像大小(KB) 我希望图像大小(KB)尽可能小 另外,当inSampleSize设置为3时,它不工作,
2048x1536
px
我第一次试着先写
它抛出了OutOfMemory异常
然后我首先将其压缩为1024x768
然后在图像上写入文本
它将图像的KB从100KB
增加到640KB
在编写文本时,我可以降低图像质量,但不能降低文本质量
当压缩质量设置为30
时,文本也会进入downsample
问题:
是否有先写后压缩
或先压缩后写
文本而不更改图像大小(KB)
我希望图像大小(KB)尽可能小
另外,当inSampleSize设置为3时,它不工作,只有2048、1024、512使用1、2、4创建的图像作为输出,我希望图像大小在700px左右,保持纵横比
代码:
冲压图像的方法
public void stampMyImage(String filePath, String text, Bitmap bitmap) {
String[] str = text.split("\n");
filePath = "/sdcard/geoTag/1imagelong_001_001_0002_0002_1135_130708144229.jpg";
bitmap = BitmapFactory.decodeFile(filePath);
if (bitmap != null) {
Bitmap dest = null;
try {
dest = bitmap.copy(bitmap.getConfig(), true);
} catch (OutOfMemoryError e1) {
Log.e("Exception", e1.getMessage());
e1.printStackTrace();
} catch (Error e) {
Log.e("Exception", e.getMessage());
e.printStackTrace();
}
Canvas cs = new Canvas(dest);
Typeface tf = Typeface.create("Verdana", Typeface.BOLD);
Paint tPaint = new Paint();
tPaint.setAntiAlias(true);
tPaint.setTextSize(40);
tPaint.setTextAlign(Align.LEFT);
tPaint.setTypeface(tf);
tPaint.setColor(Color.RED);
tPaint.setStyle(Style.FILL_AND_STROKE);
cs.drawBitmap(bitmap, 0f, 0f, null);
float textHeight = tPaint.measureText("yY");
int index = 0;
for (String Oneline : str) {
cs.drawText(Oneline, 20f, (index + 1) * textHeight + 25f,
tPaint);
index++;
}
try {
FileOutputStream fos = new FileOutputStream(
"/sdcard/timeStampedImage.jpg");
dest.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
常规压缩方法
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
int quality = 70;
myBitmapClose = BitmapFactory.decodeFile(imgUriClose.getPath(),options);
if (myBitmapClose != null) {
File sdImageMainDirectory = new File(imgUriClose.getPath());
try {
FileOutputStream fileOutputStream = new FileOutputStream(sdImageMainDirectory);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myBitmapClose.compress(CompressFormat.JPEG, quality, bos);
if (bos != null) {
bos.flush();
bos.close();
}
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
图像样本
另请参见
我遵循了一些有用的链接
您的问题是应用程序允许的内存分配。
将图像加载到位图变量(decodeFile或bitmap.copy)时,请尝试设置选项以降低图像质量。例如,在执行bitmap.copy时,请尝试如下设置选项:
bitmap.copy(Bitmap.Config.ARGB_4444, true);
您真的需要将文本保存为图像的一部分吗?我会将图像的描述保存为一个文件中的文本和图像中的字节。然后每次你想显示的时候,你都可以阅读,文本可以显示在任何屏幕上我能做什么,兄弟所有的信息都在数据库中准备好了,但我的老板想要它,所以我能做什么,是的,我想做的正是我写的,我已经在网络上使用div的背景图像显示,通过手机发送相同的错误,我已经尝试过了我使用的是bitmap.getConfig(),主要的问题是我要压缩它,首先要写和压缩有太多的东西都是以相同的方式写的,可能是某种线性行为的方法