Add DNG test, fix implementation bugs

pull/691/head
Mattia Iavarone 6 years ago
parent 6df8ad2cb2
commit 37178878a7
  1. 10
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java
  2. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraUtils.java
  3. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/action/BaseAction.java
  4. 15
      cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/ExifHelper.java
  5. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full1PictureRecorder.java
  6. 5
      cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full2PictureRecorder.java
  7. 29
      cameraview/src/test/java/com/otaliastudios/cameraview/internal/utils/ExifHelperTest.java

@ -815,7 +815,15 @@ public abstract class CameraIntegrationTest extends BaseTest {
if (camera.getCameraOptions().supports(PictureFormat.DNG)) {
camera.takePicture();
PictureResult result = waitForPictureResult(true);
// TODO assert that result.getData() is a DNG file
// assert that result.getData() is a DNG file:
// We can use the first 4 bytes assuming they are the same as a TIFF file
// https://en.wikipedia.org/wiki/List_of_file_signatures
byte[] b = result.getData();
boolean isII = b[0] == 'I' && b[1] == 'I' && b[2] == '*' && b[3] == '.';
boolean isMM = b[0] == 'M' && b[1] == 'M' && b[2] == '.' && b[3] == '*';
if (!isII && !isMM) {
throw new RuntimeException("Not a DNG file.");
}
}
}

@ -284,7 +284,7 @@ public class CameraUtils {
ExifInterface exif = new ExifInterface(stream);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
orientation = ExifHelper.readExifOrientation(exifOrientation);
orientation = ExifHelper.getOrientation(exifOrientation);
flip = exifOrientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL ||
exifOrientation == ExifInterface.ORIENTATION_FLIP_VERTICAL ||
exifOrientation == ExifInterface.ORIENTATION_TRANSPOSE ||

@ -39,7 +39,6 @@ public abstract class BaseAction implements Action {
@Override
public final void start(@NonNull ActionHolder holder) {
this.holder = holder;
holder.addAction(this);
if (holder.getLastResult(this) != null) {
onStart(holder);
@ -63,7 +62,9 @@ public abstract class BaseAction implements Action {
* holder stream.
* @param holder holder
*/
@CallSuper
protected void onStart(@NonNull ActionHolder holder) {
this.holder = holder;
// Overrideable
}

@ -11,7 +11,7 @@ public class ExifHelper {
* Maps an {@link ExifInterface} orientation value
* to the actual degrees.
*/
public static int readExifOrientation(int exifOrientation) {
public static int getOrientation(int exifOrientation) {
int orientation;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_NORMAL:
@ -34,5 +34,18 @@ public class ExifHelper {
}
return orientation;
}
/**
* Maps a degree value to {@link ExifInterface} constant.
*/
public static int getExifOrientation(int orientation) {
switch ((orientation + 360) % 360) {
case 0: return ExifInterface.ORIENTATION_NORMAL;
case 90: return ExifInterface.ORIENTATION_ROTATE_90;
case 180: return ExifInterface.ORIENTATION_ROTATE_180;
case 270: return ExifInterface.ORIENTATION_ROTATE_270;
default: throw new IllegalArgumentException("Invalid orientation: " + orientation);
}
}
}

@ -57,7 +57,7 @@ public class Full1PictureRecorder extends PictureRecorder {
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
exifRotation = ExifHelper.readExifOrientation(exifOrientation);
exifRotation = ExifHelper.getOrientation(exifOrientation);
} catch (IOException e) {
exifRotation = 0;
}

@ -23,7 +23,6 @@ import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import androidx.annotation.NonNull;
@ -93,7 +92,7 @@ public class Full2PictureRecorder extends PictureRecorder
super.onCaptureCompleted(holder, request, result);
if (mResult.format == PictureFormat.DNG) {
mDngCreator = new DngCreator(holder.getCharacteristics(this), result);
mDngCreator.setOrientation(mResult.rotation);
mDngCreator.setOrientation(ExifHelper.getExifOrientation(mResult.rotation));
if (mResult.location != null) {
mDngCreator.setLocation(mResult.location);
}
@ -148,7 +147,7 @@ public class Full2PictureRecorder extends PictureRecorder
ExifInterface exif = new ExifInterface(new ByteArrayInputStream(mResult.data));
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
mResult.rotation = ExifHelper.readExifOrientation(exifOrientation);
mResult.rotation = ExifHelper.getOrientation(exifOrientation);
} catch (IOException ignore) { }
}

@ -3,13 +3,8 @@ package com.otaliastudios.cameraview.internal.utils;
import androidx.exifinterface.media.ExifInterface;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -20,22 +15,22 @@ public class ExifHelperTest {
@Test
public void testValues() {
assertEquals(0, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_NORMAL));
assertEquals(0, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_FLIP_HORIZONTAL));
assertEquals(180, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_ROTATE_180));
assertEquals(180, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_FLIP_VERTICAL));
assertEquals(90, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_ROTATE_90));
assertEquals(90, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_TRANSPOSE));
assertEquals(270, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_ROTATE_270));
assertEquals(270, ExifHelper.readExifOrientation(ExifInterface.ORIENTATION_TRANSVERSE));
assertEquals(0, ExifHelper.getOrientation(ExifInterface.ORIENTATION_NORMAL));
assertEquals(0, ExifHelper.getOrientation(ExifInterface.ORIENTATION_FLIP_HORIZONTAL));
assertEquals(180, ExifHelper.getOrientation(ExifInterface.ORIENTATION_ROTATE_180));
assertEquals(180, ExifHelper.getOrientation(ExifInterface.ORIENTATION_FLIP_VERTICAL));
assertEquals(90, ExifHelper.getOrientation(ExifInterface.ORIENTATION_ROTATE_90));
assertEquals(90, ExifHelper.getOrientation(ExifInterface.ORIENTATION_TRANSPOSE));
assertEquals(270, ExifHelper.getOrientation(ExifInterface.ORIENTATION_ROTATE_270));
assertEquals(270, ExifHelper.getOrientation(ExifInterface.ORIENTATION_TRANSVERSE));
}
@Test
public void testUnknownValues() {
assertEquals(0, ExifHelper.readExifOrientation(-15));
assertEquals(0, ExifHelper.readExifOrientation(-1));
assertEquals(0, ExifHelper.readExifOrientation(195));
assertEquals(0, ExifHelper.readExifOrientation(Integer.MAX_VALUE));
assertEquals(0, ExifHelper.getOrientation(-15));
assertEquals(0, ExifHelper.getOrientation(-1));
assertEquals(0, ExifHelper.getOrientation(195));
assertEquals(0, ExifHelper.getOrientation(Integer.MAX_VALUE));
}
}

Loading…
Cancel
Save