Refactor Mapper class

v2
Mattia Iavarone 7 years ago
parent b9a5e4eb9e
commit 778ee7a79e
  1. 1
      MIGRATION.md
  2. 2
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java
  3. 2
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java
  4. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  5. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java
  6. 122
      cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java
  7. 85
      cameraview/src/main/java/com/otaliastudios/cameraview/Mapper1.java
  8. 2
      cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java

@ -0,0 +1 @@
# Migrating to v2

@ -153,7 +153,7 @@ public class CameraOptions1Test extends BaseTest {
}
CameraOptions o = new CameraOptions(mock(Camera.Parameters.class), false);
Mapper m = new Mapper.Mapper1();
Mapper m = new Mapper1();
Collection<Facing> s = o.getSupportedControls(Facing.class);
assertEquals(s.size(), supported.size());
for (Facing facing : s) {

@ -15,7 +15,7 @@ import static org.junit.Assert.*;
@SmallTest
public class Mapper1Test extends BaseTest {
private Mapper mapper = new Mapper.Mapper1();
private Mapper mapper = new Mapper1();
@Test
public void testMap() {

@ -50,7 +50,7 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera
Camera1(CameraView.CameraCallbacks callback) {
super(callback);
mMapper = new Mapper.Mapper1();
mMapper = new Mapper1();
}
private void schedule(@Nullable final Task<Void> task, final boolean ensureAvailable, final Runnable action) {

@ -6,7 +6,6 @@ import android.hardware.Camera;
import android.hardware.camera2.CameraCharacteristics;
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -38,7 +37,7 @@ public class CameraOptions {
@SuppressWarnings("deprecation")
CameraOptions(Camera.Parameters params, boolean flipSizes) {
List<String> strings;
Mapper mapper = new Mapper.Mapper1();
Mapper mapper = new Mapper1();
// Facing
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();

@ -25,126 +25,4 @@ abstract class Mapper {
default: return MediaRecorder.VideoEncoder.DEFAULT;
}
}
static class Mapper1 extends Mapper {
private static final HashMap<Flash, String> FLASH = new HashMap<>();
private static final HashMap<WhiteBalance, String> WB = new HashMap<>();
private static final HashMap<Facing, Integer> FACING = new HashMap<>();
private static final HashMap<Hdr, String> HDR = new HashMap<>();
static {
FLASH.put(Flash.OFF, Camera.Parameters.FLASH_MODE_OFF);
FLASH.put(Flash.ON, Camera.Parameters.FLASH_MODE_ON);
FLASH.put(Flash.AUTO, Camera.Parameters.FLASH_MODE_AUTO);
FLASH.put(Flash.TORCH, Camera.Parameters.FLASH_MODE_TORCH);
FACING.put(Facing.BACK, Camera.CameraInfo.CAMERA_FACING_BACK);
FACING.put(Facing.FRONT, Camera.CameraInfo.CAMERA_FACING_FRONT);
WB.put(WhiteBalance.AUTO, Camera.Parameters.WHITE_BALANCE_AUTO);
WB.put(WhiteBalance.INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT);
WB.put(WhiteBalance.FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT);
WB.put(WhiteBalance.DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT);
WB.put(WhiteBalance.CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
HDR.put(Hdr.OFF, Camera.Parameters.SCENE_MODE_AUTO);
if (Build.VERSION.SDK_INT >= 17) {
HDR.put(Hdr.ON, Camera.Parameters.SCENE_MODE_HDR);
} else {
HDR.put(Hdr.ON, "hdr");
}
}
@Override
<T> T map(Flash flash) {
return (T) FLASH.get(flash);
}
@Override
<T> T map(Facing facing) {
return (T) FACING.get(facing);
}
@Override
<T> T map(WhiteBalance whiteBalance) {
return (T) WB.get(whiteBalance);
}
@Override
<T> T map(Hdr hdr) {
return (T) HDR.get(hdr);
}
private <T> T reverseLookup(HashMap<T, ?> map, Object object) {
for (T value : map.keySet()) {
if (map.get(value).equals(object)) {
return value;
}
}
return null;
}
@Override
<T> Flash unmapFlash(T cameraConstant) {
return reverseLookup(FLASH, cameraConstant);
}
@Override
<T> Facing unmapFacing(T cameraConstant) {
return reverseLookup(FACING, cameraConstant);
}
@Override
<T> WhiteBalance unmapWhiteBalance(T cameraConstant) {
return reverseLookup(WB, cameraConstant);
}
@Override
<T> Hdr unmapHdr(T cameraConstant) {
return reverseLookup(HDR, cameraConstant);
}
}
static class Mapper2 extends Mapper {
@Override
<T> T map(WhiteBalance whiteBalance) {
return null;
}
@Override
<T> T map(Flash flash) {
return null;
}
@Override
<T> Flash unmapFlash(T cameraConstant) {
return null;
}
@Override
<T> WhiteBalance unmapWhiteBalance(T cameraConstant) {
return null;
}
@Override
<T> T map(Facing facing) {
return null;
}
@Override
<T> Facing unmapFacing(T cameraConstant) {
return null;
}
@Override
<T> T map(Hdr hdr) {
return null;
}
@Override
<T> Hdr unmapHdr(T cameraConstant) {
return null;
}
}
}

@ -0,0 +1,85 @@
package com.otaliastudios.cameraview;
import android.hardware.Camera;
import android.os.Build;
import java.util.HashMap;
@SuppressWarnings("unchecked")
class Mapper1 extends Mapper {
private static final HashMap<Flash, String> FLASH = new HashMap<>();
private static final HashMap<WhiteBalance, String> WB = new HashMap<>();
private static final HashMap<Facing, Integer> FACING = new HashMap<>();
private static final HashMap<Hdr, String> HDR = new HashMap<>();
static {
FLASH.put(Flash.OFF, Camera.Parameters.FLASH_MODE_OFF);
FLASH.put(Flash.ON, Camera.Parameters.FLASH_MODE_ON);
FLASH.put(Flash.AUTO, Camera.Parameters.FLASH_MODE_AUTO);
FLASH.put(Flash.TORCH, Camera.Parameters.FLASH_MODE_TORCH);
FACING.put(Facing.BACK, Camera.CameraInfo.CAMERA_FACING_BACK);
FACING.put(Facing.FRONT, Camera.CameraInfo.CAMERA_FACING_FRONT);
WB.put(WhiteBalance.AUTO, Camera.Parameters.WHITE_BALANCE_AUTO);
WB.put(WhiteBalance.INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT);
WB.put(WhiteBalance.FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT);
WB.put(WhiteBalance.DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT);
WB.put(WhiteBalance.CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
HDR.put(Hdr.OFF, Camera.Parameters.SCENE_MODE_AUTO);
if (Build.VERSION.SDK_INT >= 17) {
HDR.put(Hdr.ON, Camera.Parameters.SCENE_MODE_HDR);
} else {
HDR.put(Hdr.ON, "hdr");
}
}
@Override
<T> T map(Flash flash) {
return (T) FLASH.get(flash);
}
@Override
<T> T map(Facing facing) {
return (T) FACING.get(facing);
}
@Override
<T> T map(WhiteBalance whiteBalance) {
return (T) WB.get(whiteBalance);
}
@Override
<T> T map(Hdr hdr) {
return (T) HDR.get(hdr);
}
private <T> T reverseLookup(HashMap<T, ?> map, Object object) {
for (T value : map.keySet()) {
if (map.get(value).equals(object)) {
return value;
}
}
return null;
}
@Override
<T> Flash unmapFlash(T cameraConstant) {
return reverseLookup(FLASH, cameraConstant);
}
@Override
<T> Facing unmapFacing(T cameraConstant) {
return reverseLookup(FACING, cameraConstant);
}
@Override
<T> WhiteBalance unmapWhiteBalance(T cameraConstant) {
return reverseLookup(WB, cameraConstant);
}
@Override
<T> Hdr unmapHdr(T cameraConstant) {
return reverseLookup(HDR, cameraConstant);
}
}

@ -46,7 +46,7 @@ public class CameraUtils {
* @return true if such sensor exists
*/
public static boolean hasCameraFacing(Context context, Facing facing) {
int internal = new Mapper.Mapper1().map(facing);
int internal = new Mapper1().map(facing);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {
Camera.getCameraInfo(i, cameraInfo);

Loading…
Cancel
Save