我正在使用ZxingScannerView处理条形码扫描仪。我需要在扫描时关闭闪光灯,但相机组件已弃用,所以还有其他方法可以实现此功能。我正在附加我的代码请给我一些可能的答案,以便在zxingscanner中关闭flash.Thanks提前
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class WorkOut extends AppCompatActivity implements ZXingScannerView.ResultHandler {
Integer response = 0;
int currentIndex = 0;
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this);
Camera camera =Camera.open();
Camera.Parameters p;
p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
CameraConfigurationUtils.setTorch(p,false);
FrameLayout frameLayout = new FrameLayout(this);
FrameLayout.LayoutParams mainParam = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
frameLayout.setLayoutParams(mainParam);
//end 3
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent refresh = new Intent(WorkOut.this, MposHomeScreen.class);
startActivity(refresh);
}
});
frameLayout.addView(mScannerView);
frameLayout.addView(scanButton);
setContentView(frameLayout);
}
private void checkPermissions() {
try {
for (int i = currentIndex; i < permissions.length; i++) {
currentIndex = currentIndex + 1;
int result = ContextCompat.checkSelfPermission(this, permissions[i]);
if (result == PackageManager.PERMISSION_GRANTED) {
} else {
requestPermission(permissions[i]);
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Activity activity = this;
Context context = this;
String[] permissions = new String[]{Manifest.permission.CAMERA};
private void requestPermission(String permission) {
ActivityCompat.requestPermissions(activity, new String[]{permission},
101);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
checkPermissions();
} else {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
@Override
public void onResume() {
super.onResume();
if (response == 1) {
mScannerView = null;
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
response = 0;
}
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void onDestroy() {
super.onDestroy();
mScannerView.stopCamera();
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("ani", "onrestart");
}
@Override
public void handleResult(Result rawResult) {
Toast.makeText(getApplicationContext(), "Scanned", Toast.LENGTH_SHORT).show();
mScannerView.resumeCameraPreview(this);
}
}
答案 0 :(得分:3)
很抱歉迟到的回复......也许它还会帮助别人。
ZXingScannerView
本身有一套非常简单的设置/获取方法,用于打开/关闭相应的相机手电筒。您无需尝试使用(已弃用)Camera或Camera2。
首先要永久关闭Flash,只需在分配mScannerView
后添加以下行:
mScannerView.setFlash(false);
其次,通过按钮切换闪光灯开/关,只需在按钮的onClick方法中添加以下行:
mScannerView.setFlash(!mScannerView.getFlash());
就是这样:)它对我来说非常棒。