Skip to content

Commit cbac776

Browse files
authored
HDR support (#11)
1 parent 4e5c772 commit cbac776

File tree

10 files changed

+186
-24
lines changed

10 files changed

+186
-24
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ compile 'com.otaliastudios:cameraview:1.1.4'
3030
- Multiple capture methods
3131
- Take high-resolution pictures with `capturePicture`
3232
- Take quick snapshots as a freeze frame of the preview with `captureSnapshot` (similar to Snapchat and Instagram)
33-
- Control flash, zoom, white balance, exposure correction and more
33+
- Control HDR, flash, zoom, white balance, exposure correction and more
3434
- **Metadata** support for pictures and videos
3535
- Automatically detected orientation tags
3636
- Plug in location tags with `setLocation()` API
@@ -66,7 +66,7 @@ To use the CameraView engine, simply add a `CameraView` to your layout:
6666
android:layout_height="wrap_content" />
6767
```
6868

69-
Make sure you override `onResume`, `onPause` and `onDestroy` in your activity or fragment, and call `CameraView.start()`, `stop()` and `destroy()`.
69+
`CameraView` has lots of XML attributes, so keep reading. Make sure you override `onResume`, `onPause` and `onDestroy` in your activity or fragment, and call `CameraView.start()`, `stop()` and `destroy()`.
7070

7171
```java
7272
@Override
@@ -291,7 +291,8 @@ Most camera parameters can be controlled through XML attributes or linked method
291291
app:cameraCropOutput="false"
292292
app:cameraJpegQuality="100"
293293
app:cameraVideoQuality="480p"
294-
app:cameraWhiteBalance="auto" />
294+
app:cameraWhiteBalance="auto"
295+
app:cameraHdr="off" />
295296
```
296297

297298
|XML Attribute|Method|Values|Default Value|
@@ -304,6 +305,7 @@ Most camera parameters can be controlled through XML attributes or linked method
304305
|[`cameraJpegQuality`](#camerajpegquality)|`setJpegQuality()`|`0 <= n <= 100`|`100`|
305306
|[`cameraVideoQuality`](#cameravideoquality)|`setVideoQuality()`|`lowest` `highest` `maxQvga` `max480p` `max720p` `max1080p` `max2160p`|`max480p`|
306307
|[`cameraWhiteBalance`](#camerawhitebalance)|`setWhiteBalance()`|`auto` `incandescent` `fluorescent` `daylight` `cloudy`|`auto`|
308+
|[`cameraHdr`](#camerahdr)|`setHdr()`|`off` `on`|`off`|
307309

308310
#### cameraSessionType
309311

@@ -390,6 +392,15 @@ cameraView.setWhiteBalance(WhiteBalance.DAYLIGHT);
390392
cameraView.setWhiteBalance(WhiteBalance.CLOUDY);
391393
```
392394

395+
#### cameraHdr
396+
397+
Turns on or off HDR captures.
398+
399+
```java
400+
cameraView.setHdr(Hdr.OFF);
401+
cameraView.setHdr(Hdr.ON);
402+
```
403+
393404
## Other APIs
394405

395406
Other APIs not mentioned above are provided, and are well documented and commented in code.
@@ -465,6 +476,7 @@ This is what was done since the library was forked. I have kept the original str
465476
- *gesture framework support*
466477
- *scroll gestures support*
467478
- *MediaActionSound support*
479+
- *Hdr controls*
468480

469481
These are still things that need to be done, off the top of my head:
470482

cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void stop() {
174174
}
175175

176176
private boolean collectCameraId() {
177-
int internalFacing = mMapper.mapFacing(mFacing);
177+
int internalFacing = mMapper.map(mFacing);
178178
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
179179
for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {
180180
Camera.getCameraInfo(i, cameraInfo);
@@ -261,13 +261,34 @@ void setWhiteBalance(WhiteBalance whiteBalance) {
261261

262262
private boolean mergeWhiteBalance(Camera.Parameters params, WhiteBalance oldWhiteBalance) {
263263
if (mOptions.getSupportedWhiteBalance().contains(mWhiteBalance)) {
264-
params.setWhiteBalance((String) mMapper.mapWhiteBalance(mWhiteBalance));
264+
params.setWhiteBalance((String) mMapper.map(mWhiteBalance));
265265
return true;
266266
}
267267
mWhiteBalance = oldWhiteBalance;
268268
return false;
269269
}
270270

271+
@Override
272+
void setHdr(Hdr hdr) {
273+
Hdr old = mHdr;
274+
mHdr = hdr;
275+
if (isCameraOpened()) {
276+
synchronized (mLock) {
277+
Camera.Parameters params = mCamera.getParameters();
278+
if (mergeHdr(params, old)) mCamera.setParameters(params);
279+
}
280+
}
281+
}
282+
283+
private boolean mergeHdr(Camera.Parameters params, Hdr oldHdr) {
284+
if (mOptions.getSupportedHdr().contains(mHdr)) {
285+
params.setSceneMode((String) mMapper.map(mHdr));
286+
return true;
287+
}
288+
mHdr = oldHdr;
289+
return false;
290+
}
291+
271292
@Override
272293
void setFlash(Flash flash) {
273294
Flash old = mFlash;
@@ -283,7 +304,7 @@ void setFlash(Flash flash) {
283304

284305
private boolean mergeFlash(Camera.Parameters params, Flash oldFlash) {
285306
if (mOptions.getSupportedFlash().contains(mFlash)) {
286-
params.setFlashMode((String) mMapper.mapFlash(mFlash));
307+
params.setFlashMode((String) mMapper.map(mFlash));
287308
return true;
288309
}
289310
mFlash = oldFlash;

cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void onDeviceOrientation(int deviceOrientation) {
101101

102102
@Override
103103
void setFacing(Facing facing) {
104-
int internalFacing = mMapper.mapFacing(facing);
104+
int internalFacing = mMapper.map(facing);
105105
final String[] ids;
106106
try {
107107
ids = mCameraManager.getCameraIdList();
@@ -139,14 +139,18 @@ void setFacing(Facing facing) {
139139

140140
@Override
141141
void setFlash(Flash flash) {
142-
143142
}
144143

145144
@Override
146145
void setSessionType(SessionType sessionType) {
147146

148147
}
149148

149+
@Override
150+
void setHdr(Hdr hdr) {
151+
152+
}
153+
150154
@Override
151155
void setLocation(Location location) {
152156

cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
1717
protected WhiteBalance mWhiteBalance;
1818
protected VideoQuality mVideoQuality;
1919
protected SessionType mSessionType;
20+
protected Hdr mHdr;
2021

2122
CameraController(CameraView.CameraCallbacks callback, Preview preview) {
2223
mCameraCallbacks = callback;
@@ -37,6 +38,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
3738
abstract void setWhiteBalance(WhiteBalance whiteBalance);
3839
abstract void setVideoQuality(VideoQuality videoQuality);
3940
abstract void setSessionType(SessionType sessionType);
41+
abstract void setHdr(Hdr hdr);
4042
abstract void setLocation(Location location);
4143

4244
abstract boolean capturePicture();
@@ -59,4 +61,5 @@ abstract class CameraController implements Preview.SurfaceCallback {
5961
final WhiteBalance getWhiteBalance() { return mWhiteBalance; }
6062
final VideoQuality getVideoQuality() { return mVideoQuality; }
6163
final SessionType getSessionType() { return mSessionType; }
64+
final Hdr getHdr() { return mHdr; }
6265
}

cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.hardware.camera2.CameraCharacteristics;
77
import android.support.annotation.NonNull;
88

9+
import java.util.Collections;
910
import java.util.HashSet;
1011
import java.util.List;
1112
import java.util.Set;
@@ -18,6 +19,7 @@ public class CameraOptions {
1819
private Set<WhiteBalance> supportedWhiteBalance = new HashSet<>(5);
1920
private Set<Facing> supportedFacing = new HashSet<>(2);
2021
private Set<Flash> supportedFlash = new HashSet<>(4);
22+
private Set<Hdr> supportedHdr = new HashSet<>(2);
2123

2224
private boolean zoomSupported;
2325
private boolean videoSnapshotSupported;
@@ -59,6 +61,15 @@ public class CameraOptions {
5961
}
6062
}
6163

64+
// Hdr
65+
strings = params.getSupportedSceneModes();
66+
if (strings != null) {
67+
for (String string : strings) {
68+
Hdr value = mapper.unmapHdr(string);
69+
if (value != null) supportedHdr.add(value);
70+
}
71+
}
72+
6273
zoomSupported = params.isZoomSupported();
6374
videoSnapshotSupported = params.isVideoSnapshotSupported();
6475
autoFocusSupported = params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO);
@@ -86,7 +97,7 @@ public class CameraOptions {
8697
*/
8798
@NonNull
8899
public Set<Facing> getSupportedFacing() {
89-
return supportedFacing;
100+
return Collections.unmodifiableSet(supportedFacing);
90101
}
91102

92103

@@ -101,7 +112,7 @@ public Set<Facing> getSupportedFacing() {
101112
*/
102113
@NonNull
103114
public Set<Flash> getSupportedFlash() {
104-
return supportedFlash;
115+
return Collections.unmodifiableSet(supportedFlash);
105116
}
106117

107118

@@ -117,10 +128,22 @@ public Set<Flash> getSupportedFlash() {
117128
*/
118129
@NonNull
119130
public Set<WhiteBalance> getSupportedWhiteBalance() {
120-
return supportedWhiteBalance;
131+
return Collections.unmodifiableSet(supportedWhiteBalance);
121132
}
122133

123134

135+
/**
136+
* Set of supported hdr values.
137+
*
138+
* @see Hdr#OFF
139+
* @see Hdr#ON
140+
* @return a set of supported values.
141+
*/
142+
@NonNull
143+
public Set<Hdr> getSupportedHdr() {
144+
return Collections.unmodifiableSet(supportedHdr);
145+
}
146+
124147
/**
125148
* Whether zoom is supported. If this is false, pinch-to-zoom
126149
* will not work and {@link CameraView#setZoom(float)} will have no effect.

cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,28 @@ public Grid getGrid() {
706706
}
707707

708708

709+
/**
710+
* Controls the grids to be drawn over the current layout.
711+
*
712+
* @see Hdr#OFF
713+
* @see Hdr#ON
714+
*
715+
* @param hdr desired hdr value
716+
*/
717+
public void setHdr(Hdr hdr) {
718+
mCameraController.setHdr(hdr);
719+
}
720+
721+
722+
/**
723+
* Gets the current hdr value.
724+
* @return the current hdr value
725+
*/
726+
public Hdr getHdr() {
727+
return mCameraController.getHdr();
728+
}
729+
730+
709731
/**
710732
* Set location coordinates to be found later in the jpeg EXIF header
711733
*

cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package com.otaliastudios.cameraview;
22

33
import android.hardware.Camera;
4+
import android.os.Build;
45

56
import java.util.HashMap;
67

78
abstract class Mapper {
89

9-
abstract <T> T mapFlash(Flash internalConstant);
10-
abstract <T> T mapFacing(Facing internalConstant);
11-
abstract <T> T mapWhiteBalance(WhiteBalance internalConstant);
10+
abstract <T> T map(Flash flash);
11+
abstract <T> T map(Facing facing);
12+
abstract <T> T map(WhiteBalance whiteBalance);
13+
abstract <T> T map(Hdr hdr);
1214
abstract <T> Flash unmapFlash(T cameraConstant);
1315
abstract <T> Facing unmapFacing(T cameraConstant);
1416
abstract <T> WhiteBalance unmapWhiteBalance(T cameraConstant);
17+
abstract <T> Hdr unmapHdr(T cameraConstant);
1518

1619
static class Mapper1 extends Mapper {
1720

1821
private static final HashMap<Flash, String> FLASH = new HashMap<>();
1922
private static final HashMap<WhiteBalance, String> WB = new HashMap<>();
2023
private static final HashMap<Facing, Integer> FACING = new HashMap<>();
24+
private static final HashMap<Hdr, String> HDR = new HashMap<>();
2125

2226
static {
2327
FLASH.put(Flash.OFF, Camera.Parameters.FLASH_MODE_OFF);
@@ -31,21 +35,32 @@ static class Mapper1 extends Mapper {
3135
WB.put(WhiteBalance.FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT);
3236
WB.put(WhiteBalance.DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT);
3337
WB.put(WhiteBalance.CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
38+
HDR.put(Hdr.OFF, Camera.Parameters.SCENE_MODE_AUTO);
39+
if (Build.VERSION.SDK_INT >= 17) {
40+
HDR.put(Hdr.ON, Camera.Parameters.SCENE_MODE_HDR);
41+
} else {
42+
HDR.put(Hdr.ON, "hdr");
43+
}
44+
}
45+
46+
@Override
47+
<T> T map(Flash flash) {
48+
return (T) FLASH.get(flash);
3449
}
3550

3651
@Override
37-
<T> T mapFlash(Flash internalConstant) {
38-
return (T) FLASH.get(internalConstant);
52+
<T> T map(Facing facing) {
53+
return (T) FACING.get(facing);
3954
}
4055

4156
@Override
42-
<T> T mapFacing(Facing internalConstant) {
43-
return (T) FACING.get(internalConstant);
57+
<T> T map(WhiteBalance whiteBalance) {
58+
return (T) WB.get(whiteBalance);
4459
}
4560

4661
@Override
47-
<T> T mapWhiteBalance(WhiteBalance internalConstant) {
48-
return (T) WB.get(internalConstant);
62+
<T> T map(Hdr hdr) {
63+
return (T) HDR.get(hdr);
4964
}
5065

5166
private <T> T reverseLookup(HashMap<T, ?> map, Object object) {
@@ -71,17 +86,22 @@ <T> Facing unmapFacing(T cameraConstant) {
7186
<T> WhiteBalance unmapWhiteBalance(T cameraConstant) {
7287
return reverseLookup(WB, cameraConstant);
7388
}
89+
90+
@Override
91+
<T> Hdr unmapHdr(T cameraConstant) {
92+
return reverseLookup(HDR, cameraConstant);
93+
}
7494
}
7595

7696
static class Mapper2 extends Mapper {
7797

7898
@Override
79-
<T> T mapWhiteBalance(WhiteBalance internalConstant) {
99+
<T> T map(WhiteBalance whiteBalance) {
80100
return null;
81101
}
82102

83103
@Override
84-
<T> T mapFlash(Flash internalConstant) {
104+
<T> T map(Flash flash) {
85105
return null;
86106
}
87107

@@ -96,14 +116,24 @@ <T> WhiteBalance unmapWhiteBalance(T cameraConstant) {
96116
}
97117

98118
@Override
99-
<T> T mapFacing(Facing internalConstant) {
119+
<T> T map(Facing facing) {
100120
return null;
101121
}
102122

103123
@Override
104124
<T> Facing unmapFacing(T cameraConstant) {
105125
return null;
106126
}
127+
128+
@Override
129+
<T> T map(Hdr hdr) {
130+
return null;
131+
}
132+
133+
@Override
134+
<T> Hdr unmapHdr(T cameraConstant) {
135+
return null;
136+
}
107137
}
108138

109139
}

0 commit comments

Comments
 (0)