Error using ZXing barcode scanner via intent from my custom camera view

MoeJoe

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

Sean Owen

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to decode a barcode from camera preview using zxing library in android?

From Dev

ZXing barcode scanner in custom layout in fragment

From Dev

Integrating Zxing Barcode scanner to my android app

From Dev

Unable to get result from ZXing barcode scanner

From Dev

ZXing Barcode Scanner Intent: set DecodeHintType.ASSUME_GS1

From Dev

ZXing barcode scanner for .NET

From Dev

Error while using zxing scanner on my android app

From Dev

How to Customize the capture screen border of zxing barcode scanner from ViewFinder

From Dev

Adding objects over camera view in barcode scanner app (Swift 3)

From Dev

Google vision API Barcode Scanner remove camera view

From Dev

Android Zxing Barcode Scanner is not scanning correctly

From Dev

Core.jar not in Zxing Core folder? - Zxing Barcode Scanner

From Dev

Implement Realtime Barcode Scanner on Windows Phone 8.1 Runtime using ZXing and MFT

From Dev

How redirect to other view using Barcode-Scanner in Ionic 2

From Dev

How to restart camera efficiently in a fragment - Barcode Scanner

From Dev

Scandit Barcode Scanner Shows Blank camera on scanning

From Dev

In fragment, how to stop camera of ZXing Scanner?

From Dev

With Zxing retiring for iOS, which barcode-scanner to switch to?

From Dev

ZXing Barcode Scanner WinRT can't start StartPreviewAsync()

From Dev

ZXing Barcode Scanner WinRT can't start StartPreviewAsync()

From Dev

creating barcode scanner with zxing source code and core java

From Dev

Reading from a Barcode Scanner in Swing

From Dev

Buffering keystrokes from a barcode scanner

From Dev

Add an embedded barcode scanner onto fragment into my app via android studio

From Dev

Barcode scanning in every direction using Zbar or Zxing

From Dev

Android Generate QR code and Barcode using Zxing

From Dev

Output barcode control codes using ZXing?

From Dev

Barcode scanning in every direction using Zbar or Zxing

From Dev

How to call scanned barcode history from zxing

Related Related

  1. 1

    How to decode a barcode from camera preview using zxing library in android?

  2. 2

    ZXing barcode scanner in custom layout in fragment

  3. 3

    Integrating Zxing Barcode scanner to my android app

  4. 4

    Unable to get result from ZXing barcode scanner

  5. 5

    ZXing Barcode Scanner Intent: set DecodeHintType.ASSUME_GS1

  6. 6

    ZXing barcode scanner for .NET

  7. 7

    Error while using zxing scanner on my android app

  8. 8

    How to Customize the capture screen border of zxing barcode scanner from ViewFinder

  9. 9

    Adding objects over camera view in barcode scanner app (Swift 3)

  10. 10

    Google vision API Barcode Scanner remove camera view

  11. 11

    Android Zxing Barcode Scanner is not scanning correctly

  12. 12

    Core.jar not in Zxing Core folder? - Zxing Barcode Scanner

  13. 13

    Implement Realtime Barcode Scanner on Windows Phone 8.1 Runtime using ZXing and MFT

  14. 14

    How redirect to other view using Barcode-Scanner in Ionic 2

  15. 15

    How to restart camera efficiently in a fragment - Barcode Scanner

  16. 16

    Scandit Barcode Scanner Shows Blank camera on scanning

  17. 17

    In fragment, how to stop camera of ZXing Scanner?

  18. 18

    With Zxing retiring for iOS, which barcode-scanner to switch to?

  19. 19

    ZXing Barcode Scanner WinRT can't start StartPreviewAsync()

  20. 20

    ZXing Barcode Scanner WinRT can't start StartPreviewAsync()

  21. 21

    creating barcode scanner with zxing source code and core java

  22. 22

    Reading from a Barcode Scanner in Swing

  23. 23

    Buffering keystrokes from a barcode scanner

  24. 24

    Add an embedded barcode scanner onto fragment into my app via android studio

  25. 25

    Barcode scanning in every direction using Zbar or Zxing

  26. 26

    Android Generate QR code and Barcode using Zxing

  27. 27

    Output barcode control codes using ZXing?

  28. 28

    Barcode scanning in every direction using Zbar or Zxing

  29. 29

    How to call scanned barcode history from zxing

HotTag

Archive