【问题标题】:How to insert Exif information to a new image?如何将 Exif 信息插入新图像?
【发布时间】:2015-07-14 16:31:41
【问题描述】:

我只是在 Android 应用程序上创建来自相机的图像:

YuvImage yuv = new YuvImage(
    byteArrayDataFromCamera, 
    camera.getParameters().getPreviewFormat(),
    imageWidth, 
    imageHeight, null);

ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, imageWidth, imageHeight, 100, out);
byte[] bytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

我想在其中添加Exif information。从现在开始我已经尝试过:

// Save image on SD
storeImage(image, "Image_0.jpg");

String filePath = Environment.getExternalStorageDirectory() + "/MyFolder/Image_0.jpg";
try {
    ExifInterface exif = new ExifInterface(filePath);
    exif.setAttribute("UserComment", "my custom comment"); 
    exif.saveAttributes();
} 
catch (IOException e) {
    e.printStackTrace();
}

storeImage 方法:

private boolean storeImage(Bitmap imageData, String filename) {
    //get path to external storage (SD card)
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/MyFolder/";
    File sdIconStorageDir = new File(iconsStoragePath);

    //create storage directories, if they don't exist
    sdIconStorageDir.mkdirs();

    try {
        String filePath = sdIconStorageDir.toString() + filename;
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        //choose another format if PNG doesn't suit you
        imageData.compress(CompressFormat.PNG, 100, bos);

        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }
    return true;
}

由于我的图像是最近创建的,它没有任何ExifInterface 信息。我想知道如何将新的ExifInterface 添加到我最近创建的图像中。如何做到这一点?

【问题讨论】:

  • 你想在图片信息​​中添加什么?是否可以轮换?还是只评论?
  • 是的,对不起。只有一个自定义字符串,其中包含每个图像的一些特定信息。
  • @Sonhja 你现在可以阅读我编辑的帖子......告诉我这个消息!好编程!!
  • @MerlíPérezEscarpenter 是的,正在阅读。我会让你知道它是否有效。看起来不错的答案。我会尽力让你知道!

标签: android


【解决方案1】:

我知道您可以在图像信息中添加旋转,但要添加评论则更加复杂。 Exif 类无法正常工作......因为您可以从流中读取 Exif 信息,但您可以写入此信息。当我搜索信息以添加图像信息时,用户会遇到与您相同的问题,并且我知道现有库可以在图像信息中添加信息... 当我找到帖子时,我会给你网址!!


我可以找到帖子,阅读this!!

告诉我我是否帮助过你并且编程很好!

【讨论】:

  • 这个解决方案允许我读取元信息,但不能写它。我正在更深入地寻找..
  • 我可以在常规图像上使用它,但不能使用相机中的 byte[]。我看到相机没有给我这些信息:(但对我来说,这个解决方案有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 2012-08-04
  • 2013-11-16
  • 2012-06-16
  • 1970-01-01
  • 2020-05-24
相关资源
最近更新 更多