Reputation: 3
I've got an interesting problem for you all!
My app: I have a custom camera view, and on top of this I show the user extra information (process values and such). I also have a button on top om my camera view, and by clicking this button I'm trying to use ZXing scan-by-intent with the IntentIntegrator.java and IntentResult.java files from the ZXing library. The result from the scan should give the extra information to be shown on top of the camera view.
The problem: It seems to occur often, but not always (This is the interesting thing). When I go from my custom camera view to zxing barcode scanner app (via intent), I get the error message : "sorry the android camera encountered a problem. you may need to restart the device". This happens almost always when I'm in landscape mode in my custom camera view, and not as often when I'm in portrait mode (But is still occurs from this mode). FYI,the barcode scanner application is always running in landscape mode. I've heard so many good things about the zxing library and this scan-via-intent method, so I really just want to get rid of this error.
Here is some code from my manifest and main app:
<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" />
This is my onClick() method for the Scan button:
public void onClick(View v) {
switch (v.getId()) {
case 5:
//Scan QR button pressed
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
break;
And here's some code from my onActivityResult()-method:
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();
}
And here is my custom camera view class:
// 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();}
}
}
And finally, this is how I create my camera view in the onCreate()-method of my main class:
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);
I really hope someone has an answer to this, as I'd much rather like to move on to the more complex stages of my application :) Regards, Joar
Upvotes: 0
Views: 2483
Reputation: 66886
You are trying to use Intents to invoke Barcode Scanner as an external app, but have also pasted a large amount of project code into your app. This is entirely unnecessary and is your problem. Remove it, and instead follow the given directions in http://zxing.github.io/zxing/apidocs/com/google/zxing/integration/android/IntentIntegrator.html
Upvotes: 0