gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:36:25 28 4
gpt4 key购买 nike

我在 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(),我不会收到该错误。

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

编辑:这是我用来从文件中获取元数据的方式
    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 位置一样保留它们......

最佳答案

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

这是我的代码,它保留了图像的元数据,并摆脱了“缺少霍夫曼代码表”的东西。

关于javax.imageio.IIOException : Missing Huffman code table entry while Adding text to an jpg image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56170297/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com