开发者社区 问答 正文

Java向Exif添加新属性信息

如下就是我读取出来的Exif信息。如何用Java实现将User Comment这条属性添加到Exif信息中
screenshot

展开
收起
蛮大人123 2016-06-07 11:45:29 4377 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
     public static void main(String[] args) throws Exception {
            //原文件
            InputStream fip = new BufferedInputStream(new FileInputStream("D://nozip//2.jpg")); // No need to buffer
            LLJTran llj = new LLJTran(fip);
            try {
                llj.read(LLJTran.READ_INFO, true);
            } catch (LLJTranException e) {
                e.printStackTrace();
            }
            Exif exif = (Exif) llj.getImageInfo();      
            
            /* Set some values directly to gps IFD */
                    
            Entry e;
            // Set Latitude
            e = new Entry(Exif.ASCII);        
            e.setValue(0, 'N');
            exif.setTagValue(Exif.GPSLatitudeRef,-1, e, true);
            //设置具体的精度
             e = new Entry(Exif.RATIONAL);
            e.setValue(0, new Rational(31, 1));
            e.setValue(1, new Rational(21, 1));
            e.setValue(2, new Rational(323, 1));
            exif.setTagValue(Exif.GPSLatitude,-1, e, true);
            
            // Set Longitude
            e = new Entry(Exif.ASCII);
            e.setValue(0, 'E');
            exif.setTagValue(Exif.GPSLongitudeRef,-1, e, true);
            
            //设置具体的纬度
             e = new Entry(Exif.RATIONAL);
            e.setValue(0, new Rational(120, 1));
            e.setValue(1, new Rational(58, 1));
            e.setValue(2, new Rational(531, 1));
            exif.setTagValue(Exif.GPSLongitude,-1, e, true);
            
            llj.refreshAppx(); // Recreate Marker Data for changes done
            //改写后的文件,文件必须存在
             OutputStream out = new BufferedOutputStream(new FileOutputStream("D://nozip//1.jpg"));
            // Transfer remaining of image to output with new header.
            llj.xferInfo(null, out, LLJTran.REPLACE, LLJTran.REPLACE);
            fip.close();
            out.close();
            llj.freeMemory();
        }
    2019-07-17 19:29:43
    赞同 展开评论
问答分类:
问答地址: