我为你们所有人提出了一个有趣的问题!
我的应用: 我有一个自定义的摄像头视图,除此之外,我向用户显示了额外的信息(过程值等)。我的相机视图上还有一个按钮,点击此按钮我正在尝试使用ZXing库中的IntentIntegrator.java和IntentResult.java文件进行ZXing扫描。扫描结果应该提供额外的信息显示在摄像机视图的顶部。
问题: 它似乎经常发生,但并不总是(这是有趣的事情)。当我从我的自定义相机视图转到zxing条形码扫描仪应用程序(通过意图)时,我收到错误消息:“抱歉,Android相机遇到问题。您可能需要重启设备”。当我在自定义相机视图中处于横向模式时,几乎总是会发生这种情况,而在我处于纵向模式时则不常见(但仍然是从此模式发生)。仅供参考,条形码扫描仪应用程序始终以横向模式运行。我听说过zxing库和这种scan-via-intent方法有很多好处,所以我真的只想摆脱这个错误。
以下是我的清单和主应用程序中的一些代码:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
这是我的扫描按钮的onClick()方法:
public void onClick(View v) {
switch (v.getId()) {
case 5:
//Scan QR button pressed
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
break;
这是我的onActivityResult()方法中的一些代码 -
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode,intent);
TextView tvInf = (TextView) findViewById(7);
if(resultCode == RESULT_OK ){
// Have got scanning result
String scanContent = scanningResult.getContents();
tvInf.setBackgroundResource(R.drawable.customborder);
tvInf.getBackground().setAlpha(160);
tvInf.setText(scanContent);
}else {
// Didn't receive any scan data
tvInf.setText("");
tvInf.setVisibility(View.INVISIBLE);
Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_LONG);
toast.show();
}
这是我的自定义相机视图类:
// Class for adding camera to the custom surface view
public class CustomCameraView extends SurfaceView implements SurfaceHolder.Callback {
Camera camera;
SurfaceHolder previewHolder;
boolean previewing = false;
// Constructor :
@SuppressWarnings("deprecation")
public CustomCameraView(Context context) {
super(context);
previewHolder = this.getHolder();
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB ){
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
previewHolder.addCallback(this);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null){
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
camera.setDisplayOrientation(90);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(previewing){
camera.stopPreview();
previewing= false;
}
// Checking to see if natural device orientation is Portrait or Landscape :
if (width > height){
camera.setDisplayOrientation(0);
Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);
}else {
camera.setDisplayOrientation(90);
Parameters parameters = camera.getParameters();
parameters.setPreviewSize(height, width);
camera.setParameters(parameters);
}
try {
camera.setPreviewDisplay(previewHolder);
camera.startPreview();
previewing = true;
} catch (Exception e) {e.printStackTrace();}
}
}
最后,这就是我在主类的onCreate()方法中创建相机视图的方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Setting no title bar and full screen feature on
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Creating new frame layout:
FrameLayout frame = new FrameLayout(this.getApplicationContext());
setContentView(frame);
frame.setId(10); // FrameLayout ID=10
//Creating new camera view and adding this to frame
cv = new CustomCameraView(this.getApplicationContext());
cv.setId(11); // ID = 11
frame.addView(cv);
我真的希望有人对此有一个答案,因为我更愿意继续我的应用程序更复杂的阶段:) 问候, Joar
答案 0 :(得分:0)
您正在尝试使用Intents将Barcode Scanner作为外部应用程序调用,但也将大量项目代码粘贴到您的应用中。这完全没必要,是你的问题。删除它,然后按照http://zxing.github.io/zxing/apidocs/com/google/zxing/integration/android/IntentIntegrator.html
中的给定说明进行操作