14f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou/*
24f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * Copyright (C) 2012 The Android Open Source Project
34f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou *
44f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * Licensed under the Apache License, Version 2.0 (the "License");
54f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * you may not use this file except in compliance with the License.
64f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * You may obtain a copy of the License at
74f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou *
84f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou *      http://www.apache.org/licenses/LICENSE-2.0
94f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou *
104f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * Unless required by applicable law or agreed to in writing, software
114f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * distributed under the License is distributed on an "AS IS" BASIS,
124f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * See the License for the specific language governing permissions and
144f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou * limitations under the License.
154f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou */
164f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
174f529e7cada294befb66a1fc9b72b1aa164597dfEarl Oupackage com.android.gallery3d.exif;
184f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
194f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ouimport android.graphics.Bitmap;
204f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ouimport android.graphics.BitmapFactory;
214f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
224f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ouimport java.io.File;
234f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ouimport java.io.FileInputStream;
244f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ouimport java.io.FileOutputStream;
254f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ouimport java.io.IOException;
264f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ouimport java.io.InputStream;
274f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
28ce64bc9790075359aa99874351850384057dfa5dEarl Oupublic class ExifOutputStreamTest extends ExifXmlDataTestCase {
29ce64bc9790075359aa99874351850384057dfa5dEarl Ou    public ExifOutputStreamTest(int imageResourceId, int xmlResourceId) {
30ce64bc9790075359aa99874351850384057dfa5dEarl Ou        super(imageResourceId, xmlResourceId);
314f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou    }
324f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
334f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou    public void testExifOutputStream() throws IOException, ExifInvalidFormatException {
344f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        File file = File.createTempFile("exif_test", ".jpg");
354f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        InputStream imageInputStream = null;
364f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        InputStream exifInputStream = null;
374f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        FileInputStream reDecodeInputStream = null;
384f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        FileInputStream reParseInputStream = null;
394f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        try {
404f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            // Read the image
414f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            imageInputStream = getInstrumentation()
424f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou                    .getContext().getResources().openRawResource(mImageResourceId);
434f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            Bitmap bmp = BitmapFactory.decodeStream(imageInputStream);
444f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
454f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            // Read exif data
464f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            exifInputStream = getInstrumentation()
474f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou                    .getContext().getResources().openRawResource(mImageResourceId);
484f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            ExifData exifData = new ExifReader().read(exifInputStream);
494f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
504f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            // Encode the image with the exif data
514f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            FileOutputStream outputStream = new FileOutputStream(file);
524f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            ExifOutputStream exifOutputStream = new ExifOutputStream(outputStream);
534f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            exifOutputStream.setExifData(exifData);
544f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            bmp.compress(Bitmap.CompressFormat.JPEG, 100, exifOutputStream);
554f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            exifOutputStream.close();
564f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
574f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            // Re-decode the temp file and check the data.
584f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            reDecodeInputStream = new FileInputStream(file);
594f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            Bitmap decodedBmp = BitmapFactory.decodeStream(reDecodeInputStream);
604f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            assertNotNull(decodedBmp);
614f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou
624f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            // Re-parse the temp file the check EXIF tag
634f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            reParseInputStream = new FileInputStream(file);
644f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            ExifData reExifData = new ExifReader().read(reParseInputStream);
654f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            assertEquals(exifData, reExifData);
664f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        } finally {
674f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            Util.closeSilently(imageInputStream);
684f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            Util.closeSilently(exifInputStream);
694f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            Util.closeSilently(reDecodeInputStream);
704f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou            Util.closeSilently(reParseInputStream);
714f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou        }
724f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou    }
734f529e7cada294befb66a1fc9b72b1aa164597dfEarl Ou}