【问题标题】:Write Text on Image without increment of Size?在图像上写文本而不增加大小?
【发布时间】:2013-07-09 09:25:19
【问题描述】:

我正在图像上写一些将被压缩的文本。

图片sie2048 x 1536Something px

我首先尝试先写 它抛出 OutOfMemory 异常

那我先压缩成1024 x 768

然后在图片上写文字

它将图像 KB 从 100KB 增加到 640KB

在写文字的时候我可以compromiseImage quality but not the text Quality

在压缩质量设置为30 时,文本也变为downsample

问题:

  1. write then compressCompress then Write有没有进程
    文本而不更改 ImageSize(以 KB 为单位)?

  2. 我希望图像大小(以 KB 为单位)尽可能小?

  3. 而且当 inSampleSize 设置为 3 时,它不起作用,并且只有使用 1、2、4 的 2048 、 1024 、 512 的图像被创建为输出我想要一些大小为 700px 的图像保持纵横比。

代码:

StampingImage的方法

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();
        }

图像样本

另见

我关注的一些有用的链接

How to write text on an image in Java (Android)

Generate a image with custom text in Android

Drawing Text (TimeStamp) Over image captured from Standalone Camera

【问题讨论】:

  • 您真的需要将文本保存为图像的一部分吗?我会将图像的描述作为文本和图像中的字节保存在一个文件中。然后每次你想显示你都阅读,文本可以显示到任何屏幕
  • 我能做什么兄弟所有信息都已在数据库中准备好但我的老板想要它所以我能做什么是的我想做我写的我已经在 div 的背景中使用图像显示在网络上通过移动设备发送

标签: android optimization android-canvas bitmapimage image-compression


【解决方案1】:

您的问题是您的应用程序允许的分配内存。 当您将图像加载到位图变量(decodeFile 或 bitmap.copy)时,请尝试设置选项以降低图像质量。例如,当您执行 bitmap.copy 时,尝试设置如下选项:

bitmap.copy(Bitmap.Config.ARGB_4444, true);

【讨论】:

  • 我已经尝试过同样的错误,所以我选择了 bitmap.getConfig()
  • 主要问题是我要压缩它,首先要编写和压缩有太多东西都以相同的方式编写,也许是某种线性行为的方法
猜你喜欢
  • 2020-08-26
  • 1970-01-01
  • 2021-11-27
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
相关资源
最近更新 更多