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

javax.imageio.IIOException: Missing Huffman code table entry while Adding text to an jpg image

我正在使用 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(真);有效,但它会重置所有元数据,我想像 gps 位置一样保留它们...

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

这是我的代码,它保留图像的元数据,并删除 "missing huffman code table" 东西。