javax.imageio.IIOException:将文本添加到jpg图像时缺少霍夫曼代码表条目

时间:2019-05-16 13:57:35

标签: java android bitmap jpeg

我正在使用以下代码在Android中向图像添加文本:

public Bitmap drawTextToBitmap(Context gContext, Bitmap image, String gText) {

    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;

    android.graphics.Bitmap.Config bitmapConfig =
            image.getConfig();
    // set default bitmap config if none
    if(bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one

    Bitmap bitmap = null;

    try{
        bitmap = image.copy(bitmapConfig, true);
        image.recycle();

        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #3D3D3D
        paint.setColor(Color.WHITE);
        // text size in pixels
        paint.setTextSize((int) (50 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(gText, 0, gText.length(), bounds);
        int padding = bounds.height()/2;
        int x = bitmap.getWidth() - (bounds.width()+padding);
        int y = (bitmap.getHeight() - (bounds.height()+padding));

        canvas.drawText(gText, x, y, paint);
    }catch (Throwable e){
        AppLog.e("DrawingBitmap","error while adding timestamp",e);
    }
    return bitmap;
}

然后我用转换后的位图创建一个新文件

storeImage(newBitmap, newFileName);

 private File storeImage(Bitmap image, String nameFile) {

    File pictureFile = new File(getExternalCacheDir(), nameFile);

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        AppLog.e("error creating bitmap", "File not found: " + e.getMessage());
    } catch (IOException e) {
        AppLog.e("error creating bitmap", "Error accessing file: " + e.getMessage());
    }

    return pictureFile;
}

我将文件发送到服务器,接收输入流,创建文件,缩放文件,并使用缩放后的图像创建新文件:

ImageWriter.write(metadata, new IIOImage(image, null, metadata), param);

我收到一个IIOException:

 javax.imageio.IIOException: Missing Huffman code table entry
    at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1067)
    at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:363)
    at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:162)

如果我不从android调用drawTextToBitmap(),则不会收到该错误。

如果有人可以帮助我...谢谢

编辑:这是我从文件中获取元数据的方式

    private static IIOMetadata readMetaData(File source)  {
    try {
        ImageInputStream stream = ImageIO.createImageInputStream(source);
        Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
        IIOMetadata metadata = null;
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            reader.setInput(stream);
            metadata = reader.getImageMetadata(0);
        }
        return metadata;
    }catch (Exception e){
        _logger.error(e);
        e.printStackTrace();
    }
    return null;
}

编辑2:

使用jpegParams.setOptimizeHuffmanTables(true);可以,但是它会重置所有元数据,我想让它们像gps位置一样...

1 个答案:

答案 0 :(得分:2)

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
if (param instanceof JPEGImageWriteParam) {
    ((JPEGImageWriteParam) param).setOptimizeHuffmanTables(true);
}
IIOImage image = new IIOImage(reader.read(0), null, reader.getImageMetadata(0));
writer.write(null, image, param);

这是我的代码,该代码保留图像的元数据,并摆脱“缺少霍夫曼代码表”的内容。