How to fix exif warnings and problems in JPG files?

让人想犯罪 __ 提交于 2019-12-24 23:15:29

问题


I wrote my own camera app. This app writes exif information in the jpg files. It works well but I have some problems with the exifInferface class, e.g., I get the following errors when re-read the JPG file:

Warning Invalid EXIF text encoding 
Warning Invalid size (8589934590) for IFD0 tag 0x8827 
Warning Bad IFD1 directory 

I know that my IFD0 pointer at the exif-Information is broken. It may be that while writing the exif information the pointer is broken?

However, I've Googled and found nothing

I use this class to read an write the exif-informations:

public class ExifHelper {
private String aperature = null;
private String exposureTime = null;
private String flash = null;
private String focalLength = null;
private String gpsAltitude = null;
private String gpsAltitudeRef = null;
private String gpsDateStamp = null;
private String gpsLatitude = null;
private String gpsLatitudeRef = null;
private String gpsLongitude = null;
private String gpsLongitudeRef = null;
private String gpsProcessingMethod = null;
private String gpsTimestamp = null;
private String iso = null;
private String make = null;
private String model = null;
private String imageLength = null;
private String imageWidth = null;
private String orientation = null;
private String whiteBalance = null;  
private String exifVersion = null;
private String time = null;

private ExifInterface inFile = null;
private ExifInterface outFile = null;



final static String TAG = "ExifHelper";
/**
 * The file before it is compressed
 * 
 * @param filePath 
 * @throws IOException
 */
public void createInFile(String filePath) throws IOException {
    this.inFile = new ExifInterface(filePath);       
}

/** 
 * The file after it has been compressed
 * 
 * @param filePath
 * @throws IOException
 */
public void createOutFile(String filePath) throws IOException {
    this.outFile = new ExifInterface(filePath);
}

/**
 * Reads all the EXIF data from the input file.
 */
public void readExifData() {
    this.aperature = inFile.getAttribute(ExifInterface.TAG_APERTURE);
    this.exposureTime = inFile.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);
    this.flash = inFile.getAttribute(ExifInterface.TAG_FLASH);
    this.focalLength = inFile.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
    this.gpsAltitude = inFile.getAttribute(ExifInterface.TAG_GPS_ALTITUDE);
    this.gpsAltitudeRef = inFile.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF);
    this.gpsDateStamp = inFile.getAttribute(ExifInterface.TAG_GPS_DATESTAMP);
    this.gpsLatitude = inFile.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
    this.gpsLatitudeRef = inFile.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
    this.gpsLongitude = inFile.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
    this.gpsLongitudeRef = inFile.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
    this.gpsProcessingMethod = inFile.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD);
    this.gpsTimestamp = inFile.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
    this.imageLength = inFile.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
    this.imageWidth = inFile.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
    this.iso = inFile.getAttribute(ExifInterface.TAG_ISO);
    this.make = inFile.getAttribute(ExifInterface.TAG_MAKE);
    this.model = inFile.getAttribute(ExifInterface.TAG_MODEL);
    this.orientation = inFile.getAttribute(ExifInterface.TAG_ORIENTATION);
    this.whiteBalance = inFile.getAttribute(ExifInterface.TAG_WHITE_BALANCE);  
    this.exifVersion = inFile.getAttribute("ExifVersion");
}

/**
 * Writes the previously stored EXIF data to the output file. 
 * @param pictureDate 
 * @param orientationValues 
 * @param accelValues 
 * 
 * @throws IOException
 */
public void writeExifData(String pictureDate) throws IOException {
    // Don't try to write to a null file
    if (this.outFile == null) {
        return;
    }

    if (this.aperature != null) {
        this.outFile.setAttribute(ExifInterface.TAG_APERTURE, this.aperature);
    }
    if (this.exposureTime != null) {
        this.outFile.setAttribute(ExifInterface.TAG_EXPOSURE_TIME, this.exposureTime);
    }
    if (this.flash != null) {
        this.outFile.setAttribute(ExifInterface.TAG_FLASH, this.flash);
    }
    if (this.focalLength != null) {
        this.outFile.setAttribute(ExifInterface.TAG_FOCAL_LENGTH, this.focalLength);
    }
    if (this.gpsAltitude != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_ALTITUDE, this.gpsAltitude);
    }
    if (this.gpsAltitudeRef != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF, this.gpsAltitudeRef);
    }
    if (this.gpsDateStamp != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_DATESTAMP, this.gpsDateStamp);
    }
    if (this.gpsLatitude != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_LATITUDE, this.gpsLatitude);
    }
    if (this.gpsLatitudeRef != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, this.gpsLatitudeRef);
    }
    if (this.gpsLongitude != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, this.gpsLongitude);
    }
    if (this.gpsLongitudeRef != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, this.gpsLongitudeRef);
    }
    if (this.gpsProcessingMethod != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD, this.gpsProcessingMethod);
    }
    if (this.gpsTimestamp != null) {
        this.outFile.setAttribute(ExifInterface.TAG_GPS_TIMESTAMP, this.gpsTimestamp);
    }
    if (this.iso != null) {
        this.outFile.setAttribute(ExifInterface.TAG_ISO, this.iso);
    }
    if (this.make != null) {
        this.outFile.setAttribute(ExifInterface.TAG_MAKE, this.make);
    }
    if (this.model != null) {
        this.outFile.setAttribute(ExifInterface.TAG_MODEL, this.model);
    }
    if (this.orientation != null) {
        this.outFile.setAttribute(ExifInterface.TAG_ORIENTATION, this.orientation);
    }
    if (this.whiteBalance != null) {
        this.outFile.setAttribute(ExifInterface.TAG_WHITE_BALANCE, this.whiteBalance);
    }
    if (this.exifVersion != null) {
        this.outFile.setAttribute("ExifVersion", this.exifVersion);
    }


    this.outFile.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, this.imageLength);    
    this.outFile.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, this.imageWidth);
    this.outFile.setAttribute(ExifInterface.TAG_DATETIME, pictureDate);
    String mString = exifRandomZahlen();
    this.outFile.setAttribute("UserComment", mString);

    this.outFile.saveAttributes();
}

I also write 2 images form the takepicture-method. One the original there is no problem and the second with modify exif informations. There is the problem with the exifinterface I think by writing it back to the JPG.

I used the tool DumpImage to view the exif informations. This tool is from the metaworking group (www.metadataworkinggroup.org)

So I have a big question, How I can fix this broken exif data? For example the IDF0 pointer somebody know or have the same problem?

I get, for example, the Tag TAG_DATETIME two times in my exif information

this is the class for saveing the photo:

public class Photo extends Activity implements PictureCallback {
public interface OnPictureTakenListener {
    void pictureTaken(File pictureFile, File pictureFilePatched, String exifDateString);
}

private final Context context;
private OnPictureTakenListener listener;

public Photo(Context ourContext, OnPictureTakenListener theListener) {
    this.context = ourContext;
    this.listener = theListener;
}

@SuppressLint("SimpleDateFormat")
@Override
public void onPictureTaken(byte[] data, Camera camera) {

    Date date = new Date();

    File pictureFileDir = getDir();

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs())

    {
        Log.d(AndroidCamera.DEBUG_TAG,
                "Can't create directory to save image.");
        Toast.makeText(context, "Can't create directory to save image.",
                Toast.LENGTH_LONG).show();
        return;
    }

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    SimpleDateFormat dateConverter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");

    String exifDateString = dateFormat.format(date);

    String datePicture = dateConverter.format(date); 

    String photoFile = "Picture_" + datePicture + ".jpg";

    String photoFilePatched = "Picture_" + datePicture + "_patched.jpg";

    String filename = pictureFileDir.getPath() + File.separator + photoFile;

    String filenamePatched =  pictureFileDir.getPath() + File.separator + photoFilePatched;

    File pictureFile = new File(filename);

    File pictureFilePatched = new File (filenamePatched);

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        FileOutputStream fosPatched = new FileOutputStream(pictureFilePatched);
        fos.write(data);
        fosPatched.write(data);
        fos.close();
        fosPatched.close();



        Toast.makeText(context, "New Image saved:" + photoFile,
                Toast.LENGTH_LONG).show();
    } catch (Exception error) {
        Log.d(AndroidCamera.DEBUG_TAG, "File" + filename + " not saved: "
                + error.getMessage());
        Toast.makeText(context, "Image could not be saved.",
                Toast.LENGTH_LONG).show();
    }

    listener.pictureTaken(pictureFile,pictureFilePatched,exifDateString);
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

}


private File getDir() {
    File sdDir = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    return new File(sdDir, "Camera");
}

static final int REQUEST_IMAGE_CAPTURE = 1;

}

here in main activity i call on one from the two pictures to write the exif infortmations:

camera.takePicture(null, null, new Photo(this,
            new Photo.OnPictureTakenListener() {
                public void pictureTaken(final File pictureFile,final File pictureFilePatched , final String date) {

                    final String fileName = pictureFile.getPath();
                    final String fileNamePatched = pictureFilePatched.getPath();
                    final String dateTime = date;
                    // don't start picture preview immediately, but a little
                    // delayed...
                    continueWithPreview.postDelayed(new Runnable() {

                        @Override
                        public void run() {

                                try {
                                // EXIF Matadata change
                                    ExifHelper exifHelper = new ExifHelper();
                                    exifHelper.createInFile(fileName);
                                    //EXIF Metadata read
                                    exifHelper.readExifData();
                                    exifHelper.createOutFile(fileNamePatched);  
                                    //Exif Metadata write
                                    exifHelper.writeExifData(dateTime);


                            } catch (IOException e) {
                                e.printStackTrace();
                                Log.e("PictureActivity", e.getLocalizedMessage());
                            } 

                            if (null != camera)
                            {
                                camera.startPreview();
                                Toast.makeText(AndroidCamera.this, "started!",
                                        Toast.LENGTH_SHORT).show();
                            }

                        }
                    }, 2500);

来源:https://stackoverflow.com/questions/23521554/how-to-fix-exif-warnings-and-problems-in-jpg-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!