Skip to content
This repository has been archived by the owner on Apr 21, 2024. It is now read-only.

Commit

Permalink
Fix 散光灯开启后进入相册后闪光灯状态错误
Browse files Browse the repository at this point in the history
  • Loading branch information
XuDaojie committed Sep 21, 2016
1 parent f60f48f commit dd43c96
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ public class CaptureActivity extends Activity implements Callback {
private boolean playBeep;
private static final float BEEP_VOLUME = 0.10f;
private boolean vibrate;
private boolean allowOpenLight = true;
private boolean flashLightOpen = false;
private ImageView backIbtn;
private ImageButton flashIbtn;
private TextView galleryTv;

private CameraManager.FlashLightListener flashLightListener;

/**
* Called when the activity is first created.
Expand All @@ -84,24 +85,20 @@ public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.qr_camera);

mActivity = this;
//ViewUtil.addTopView(getApplicationContext(), this, R.string.scan_card);
CameraManager.init(getApplication());
backIbtn = (ImageView) findViewById(R.id.back_ibtn);
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
flashIbtn = (ImageButton) findViewById(R.id.flash_ibtn);
galleryTv = (TextView) findViewById(R.id.gallery_tv);
hasSurface = false;
inactivityTimer = new InactivityTimer(this);

CameraManager.init(getApplication());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
REQUEST_PERMISSION_CAMERA);
}
}

initView();
}

@Override
Expand All @@ -125,39 +122,6 @@ protected void onResume() {
}
initBeepSound();
vibrate = true;

backIbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mActivity.finish();
}
});
flashIbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (allowOpenLight) {
CameraManager.get().setFlashLight(true);
flashIbtn.setImageResource(R.drawable.ic_flash_on_white_24dp);
} else {
CameraManager.get().setFlashLight(false);
flashIbtn.setImageResource(R.drawable.ic_flash_off_white_24dp);
}
allowOpenLight = !allowOpenLight;
}
});
galleryTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSION_PHOTO);
} else {
ActionUtils.startActivityForGallery(mActivity, ActionUtils.PHOTO_REQUEST_GALLERY);
}
}
});
}

@Override
Expand All @@ -167,6 +131,9 @@ protected void onPause() {
handler.quitSynchronously();
handler = null;
}
if (flashLightOpen) {
setFlashLightOpen(false);
}
CameraManager.get().closeDriver();
}

Expand Down Expand Up @@ -251,7 +218,6 @@ protected void handleResult(String resultString) {
if (resultString.equals("")) {
Toast.makeText(CaptureActivity.this, R.string.scan_failed, Toast.LENGTH_SHORT).show();
} else {
// System.out.println("Result:" + resultString);
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("result", resultString);
Expand All @@ -261,6 +227,94 @@ protected void handleResult(String resultString) {
mActivity.finish();
}

protected void initView() {
setContentView(R.layout.qr_camera);
backIbtn = (ImageView) findViewById(R.id.back_ibtn);
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
flashIbtn = (ImageButton) findViewById(R.id.flash_ibtn);
galleryTv = (TextView) findViewById(R.id.gallery_tv);

setFlashLightListener(new CameraManager.FlashLightListener() {
@Override
public void onOpen() {
flashIbtn.setImageResource(R.drawable.ic_flash_on_white_24dp);
}

@Override
public void onOff() {
flashIbtn.setImageResource(R.drawable.ic_flash_off_white_24dp);
}
});
backIbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mActivity.finish();
}
});
flashIbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleFlashLight();
}
});
galleryTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSION_PHOTO);
} else {
ActionUtils.startActivityForGallery(mActivity, ActionUtils.PHOTO_REQUEST_GALLERY);
}
}
});
}

/**
* 切换散光灯状态
*/
public void toggleFlashLight() {
if (flashLightOpen) {
setFlashLightOpen(false);
} else {
setFlashLightOpen(true);
}
}

/**
* 设置闪光灯是否打开
* @param open
*/
public void setFlashLightOpen(boolean open) {
if (flashLightOpen == open) return;

if (flashLightOpen) {
if (flashLightListener != null) {
flashLightListener.onOff();
}
} else {
if (flashLightListener != null) {
flashLightListener.onOpen();
}
}
flashLightOpen = !flashLightOpen;
CameraManager.get().setFlashLight(open);
}

/**
* 当前散光灯是否打开
* @return
*/
public boolean isFlashLightOpen() {
return flashLightOpen;
}

public void setFlashLightListener(CameraManager.FlashLightListener lightListener) {
this.flashLightListener = lightListener;
}

private void initCamera(SurfaceHolder surfaceHolder) {
try {
CameraManager.get().openDriver(surfaceHolder);
Expand All @@ -276,9 +330,8 @@ private void initCamera(SurfaceHolder surfaceHolder) {
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,12 @@ public int checkCameraPermission() {
public boolean isPreviewing() {
return previewing;
}

/**
* 监听散光灯状态
*/
public interface FlashLightListener {
void onOpen();
void onOff();
}
}
2 changes: 1 addition & 1 deletion qrcodelib/src/main/res/layout/qr_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xdj:qr_angleColor="@android:color/holo_blue_dark"
xdj:qr_errorHint="请允许访问摄像头后重试"
xdj:qr_hint="将二维码/条形码置于框内即自动扫描"
xdj:qr_errorHint="请允许访问摄像头后重试"
xdj:qr_textHintColor="@android:color/darker_gray"
xdj:qr_textErrorHintColor="@android:color/white"/>

Expand Down

0 comments on commit dd43c96

Please sign in to comment.