Home:ALL Converter>Android camera2 api and exif

Android camera2 api and exif

Ask Time:2015-02-17T12:15:51         Author:moleculeone

Json Formatter

I am experimenting with the camera2 api and I have made an app that can take an a photo from the camera. Now I want to add exif data to the captured image. I have a question on where or how to put the exif information.

Should I create an Exif interface in the onCaptureCompleted() function or what is the best way to do it?

final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {

    @Override
    public void onCaptureCompleted(CameraCaptureSession session,
                                   CaptureRequest request, TotalCaptureResult result) {

        super.onCaptureCompleted(session, request, result);
        Toast.makeText(MainActivity.this, "Saved:"+file, Toast.LENGTH_SHORT).show();


        ExifInterface exifTags = null;
        try {
            exifTags = new ExifInterface(file.getCanonicalPath());

            exifTags.setAttribute(ExifInterface.TAG_GPS_LATITUDE, Double.toString(cur_lat));
            exifTags.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, Double.toString(cur_long));

            exifTags.saveAttributes();

        } catch (IOException e) {
            e.printStackTrace();
        }
        //System.out.println(file.getCanonicalPath());
        System.out.println("Exif Test: " + Double.toString(cur_lat) + " " + Double.toString(cur_lat));

    }

};

When I do this I get an error:

"ImageReader_JNI﹕ Unable to acquire a lockedBuffer, very likely client tries to lock more than maxImages buffers"

What is the best way to do this? Any suggestion would be very helpful.

Author:moleculeone,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28554546/android-camera2-api-and-exif
Maxim Metelskiy :

What image format you try to capture? If JPEG, then all Exif tags are should be already written in image.\nThe result image is delivered in OnImageAvailableListener.onImageAvailable(), not in CameraCaptureSession. onCaptureCompleted().\nTry to add your custom tags in onImageAvailable method.\n\n\n EDIT:\n\n\n@Override\n public void onImageAvailable(ImageReader reader) {\n Log.e(\"TAG\", System.currentTimeMillis() + \"\");\n Image mImage = reader.acquireNextImage();\n ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();\n byte[] bytes = new byte[buffer.remaining()];\n buffer.get(bytes);\n FileOutputStream output = null;\n try {\n output = new FileOutputStream(mFile);\n output.write(bytes);\n } catch (FileNotFoundException e) {\n e.printStackTrace();\n } catch (IOException e) {\n e.printStackTrace();\n } finally {\n mImage.close();\n if (null != output) {\n try {\n output.close();\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n }\n\n try {\n ExifInterface exif = new ExifInterface(mFile.getAbsolutePath());\n exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, \"10\");\n exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, \"10\");\n exif.saveAttributes();\n } catch (IOException e) {\n e.printStackTrace();\n }\n\n }\n",
2015-02-17T16:43:28
yy