Skip to content

Commit 72d7434

Browse files
committed
trying to fix camera setParameters crash on some devices
1 parent ae705a7 commit 72d7434

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

camerakit/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
33
ext {
44
PUBLISH_GROUP_ID = 'com.flurgle'
55
PUBLISH_ARTIFACT_ID = 'camerakit'
6-
PUBLISH_VERSION = '0.9.23'
6+
PUBLISH_VERSION = '0.9.24'
77
}
88

99
android {
@@ -32,12 +32,12 @@ android {
3232
}
3333

3434
dependencies {
35-
implementation 'com.android.support:appcompat-v7:26.0.2'
35+
implementation 'com.android.support:appcompat-v7:26.1.0'
3636

3737

38-
compile 'android.arch.lifecycle:runtime:1.0.0-alpha9'
39-
compile 'android.arch.lifecycle:extensions:1.0.0-alpha9'
40-
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha9'
38+
compile 'android.arch.lifecycle:runtime:1.0.0-alpha9-1'
39+
compile 'android.arch.lifecycle:extensions:1.0.0-alpha9-1'
40+
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha9-1'
4141
}
4242

4343
apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle'

camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
import android.media.MediaRecorder;
88
import android.os.Build;
99
import android.os.Handler;
10+
import android.support.v4.util.SparseArrayCompat;
1011
import android.util.Log;
1112
import android.support.annotation.Nullable;
1213
import android.view.MotionEvent;
1314
import android.view.SurfaceHolder;
1415
import android.view.View;
1516

17+
import com.flurgle.camerakit.CameraKit.Constants;
18+
1619
import java.io.File;
1720
import java.io.IOException;
1821
import java.util.ArrayList;
@@ -37,6 +40,16 @@ public class Camera1 extends CameraImpl {
3740
private static final int FOCUS_METERING_AREA_WEIGHT_DEFAULT = 1000;
3841
private static final int DELAY_MILLIS_BEFORE_RESETTING_FOCUS = 3000;
3942

43+
private static final SparseArrayCompat<String> FLASH_MODES = new SparseArrayCompat<>();
44+
45+
static {
46+
FLASH_MODES.put(Constants.FLASH_OFF, Camera.Parameters.FLASH_MODE_OFF);
47+
FLASH_MODES.put(Constants.FLASH_ON, Camera.Parameters.FLASH_MODE_ON);
48+
FLASH_MODES.put(Constants.FLASH_TORCH, Camera.Parameters.FLASH_MODE_TORCH);
49+
FLASH_MODES.put(Constants.FLASH_AUTO, Camera.Parameters.FLASH_MODE_AUTO);
50+
}
51+
52+
4053
private int mCameraId;
4154
private Camera mCamera;
4255
private Camera.Parameters mCameraParameters;
@@ -135,25 +148,18 @@ void setFacing(@Facing int facing) {
135148
}
136149
}
137150

151+
138152
@Override
139153
void setFlash(@Flash int flash) {
140-
if (mCameraParameters != null) {
141-
List<String> flashes = mCameraParameters.getSupportedFlashModes();
142-
String internalFlash = new ConstantMapper.Flash(flash).map();
143-
if (flashes != null && flashes.contains(internalFlash)) {
144-
mCameraParameters.setFlashMode(internalFlash);
145-
mFlash = flash;
146-
} else {
147-
String currentFlash = new ConstantMapper.Flash(mFlash).map();
148-
if (flashes == null || !flashes.contains(currentFlash)) {
149-
mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
150-
mFlash = FLASH_OFF;
151-
}
154+
if (flash == mFlash) {
155+
return;
156+
}
157+
try {
158+
if (setFlashInternal(flash)) {
159+
mCamera.setParameters(mCameraParameters);
152160
}
153-
154-
mCamera.setParameters(mCameraParameters);
155-
} else {
156-
mFlash = flash;
161+
} catch (Exception ex){
162+
ex.printStackTrace();
157163
}
158164
}
159165

@@ -581,51 +587,51 @@ private void prepareMediaRecorder() {
581587
private CamcorderProfile getCamcorderProfile(@VideoQuality int videoQuality) {
582588
CamcorderProfile camcorderProfile = null;
583589
switch (videoQuality) {
584-
case CameraKit.Constants.VIDEO_QUALITY_QVGA:
590+
case Constants.VIDEO_QUALITY_QVGA:
585591
if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_QVGA)) {
586592
camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_QVGA);
587593
} else {
588-
return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_LOWEST);
594+
return getCamcorderProfile(Constants.VIDEO_QUALITY_LOWEST);
589595
}
590596
break;
591597

592-
case CameraKit.Constants.VIDEO_QUALITY_480P:
598+
case Constants.VIDEO_QUALITY_480P:
593599
if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_480P)) {
594600
camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_480P);
595601
} else {
596-
return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_QVGA);
602+
return getCamcorderProfile(Constants.VIDEO_QUALITY_QVGA);
597603
}
598604
break;
599605

600-
case CameraKit.Constants.VIDEO_QUALITY_720P:
606+
case Constants.VIDEO_QUALITY_720P:
601607
if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_720P)) {
602608
camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_720P);
603609
} else {
604-
return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_480P);
610+
return getCamcorderProfile(Constants.VIDEO_QUALITY_480P);
605611
}
606612
break;
607613

608-
case CameraKit.Constants.VIDEO_QUALITY_1080P:
614+
case Constants.VIDEO_QUALITY_1080P:
609615
if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_1080P)) {
610616
camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_1080P);
611617
} else {
612-
return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_720P);
618+
return getCamcorderProfile(Constants.VIDEO_QUALITY_720P);
613619
}
614620
break;
615621

616-
case CameraKit.Constants.VIDEO_QUALITY_2160P:
622+
case Constants.VIDEO_QUALITY_2160P:
617623
try {
618624
camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_2160P);
619625
} catch (Exception e) {
620-
return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_HIGHEST);
626+
return getCamcorderProfile(Constants.VIDEO_QUALITY_HIGHEST);
621627
}
622628
break;
623629

624-
case CameraKit.Constants.VIDEO_QUALITY_HIGHEST:
630+
case Constants.VIDEO_QUALITY_HIGHEST:
625631
camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_HIGH);
626632
break;
627633

628-
case CameraKit.Constants.VIDEO_QUALITY_LOWEST:
634+
case Constants.VIDEO_QUALITY_LOWEST:
629635
camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_LOW);
630636
break;
631637
}
@@ -764,5 +770,28 @@ private static int calculateCenter(float coord, int dimen, int buffer) {
764770
return normalized;
765771
}
766772
}
767-
773+
/**
774+
* @return {@code true} if {@link #mCameraParameters} was modified.
775+
*/
776+
private boolean setFlashInternal(int flash) {
777+
if (isCameraOpened()) {
778+
List<String> modes = mCameraParameters.getSupportedFlashModes();
779+
String mode = FLASH_MODES.get(flash);
780+
if (modes != null && modes.contains(mode)) {
781+
mCameraParameters.setFlashMode(mode);
782+
mFlash = flash;
783+
return true;
784+
}
785+
String currentMode = FLASH_MODES.get(mFlash);
786+
if (modes == null || !modes.contains(currentMode)) {
787+
mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
788+
mFlash = Constants.FLASH_OFF;
789+
return true;
790+
}
791+
return false;
792+
} else {
793+
mFlash = flash;
794+
return false;
795+
}
796+
}
768797
}

demo/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ android {
2121

2222
dependencies {
2323
compile project(':camerakit')
24-
implementation 'com.android.support:appcompat-v7:26.0.2'
24+
implementation 'com.android.support:appcompat-v7:26.1.0'
2525
compile 'com.jakewharton:butterknife:8.4.0'
2626
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
2727
}

0 commit comments

Comments
 (0)