在图像上写文字而不增加大小?

时间:2013-07-09 09:25:19

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

我正在写一些将被压缩的Image文本。

图片sie 2048 x 1536某事px

我首先尝试先写     它会抛出OutOfMemory Exception

然后我首先将其压缩为1024 x 768

然后在图像上写文字

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

在撰写文字时我可以compromise Image 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维持宽高比的图像。

  4. 代码:

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

    图像样本 The Sample Image with Text Written on it

    另见

    我遵循的一些有用的链接

    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

1 个答案:

答案 0 :(得分:0)

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

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