Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am adding text to an image using this code in 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;
}

Then I create a new File with the transformed 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;
}

I send the file to my server, I receive an input stream, I create a File, I scale it and I create a new File with the scaled image :

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

I get an 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)

if I don't call drawTextToBitmap() from android I don't get that error.

If someone can help me ... thx

EDIT : here is the way I use to get metadata from my file

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

Edit 2 :

Using jpegParams.setOptimizeHuffmanTables(true); works but it resets all metadata and I want to keep them like gps location ...

like image 603
Alays Avatar asked May 16 '19 13:05

Alays


1 Answers

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

here's my code, which keeps the metadata of image, and get rid of "missing huffman code table" stuff.

like image 110
mysh Avatar answered Nov 04 '22 11:11

mysh