Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Huawei P9 it not work #287

Closed
WeRockStar opened this issue Mar 23, 2017 · 20 comments
Closed

Huawei P9 it not work #287

WeRockStar opened this issue Mar 23, 2017 · 20 comments
Labels

Comments

@WeRockStar
Copy link
Contributor

Exception: Camera new cameraInitNormal: 0

@vickyleu
Copy link

我这边也一样,不报异常但是什么都识别不了,努比亚的手机也是,识别率奇低,三重识别都没用,麻烦的是我们的产品是面向工厂的,工厂老板都爱用华为手机,头疼

@WeRockStar
Copy link
Contributor Author

@vickyleu English please. :)

@dm77
Copy link
Owner

dm77 commented Mar 25, 2017

Hmm based on similar bug report here https://github.com/oney/react-native-webrtc/issues/215, tend to think it might have to do something with deprecated Camera api.

Since I don't have a P9 device with me, would it be possible for you to make sure your app first has the camera permissions? Thank you.

@WeRockStar
Copy link
Contributor Author

@dm77 I'm sure has granted camera permission.

@danielcerny
Copy link

@dm77 Granted, for sure. Probably deprecated Camera1 API. Camera2 API usage needed.

@WeRockStar
Copy link
Contributor Author

Now. I fix this problem with PR #293.

@vagrant1991
Copy link

getOptimalPreviewSize method at CameraPreview , the algorithm is wrong.

@vagrant1991
Copy link

@dm77 width 1080 height 1572 huawei p9, the result is 144 * 208

@vagrant1991
Copy link

change the if statement

if (Math.abs(size.height - targetHeight) < minDiff && size.height >= 480) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}

now is ok, point is size.height >= 480

@dm77
Copy link
Owner

dm77 commented Jul 13, 2017

Thanks @vagrant1991

@dm77
Copy link
Owner

dm77 commented Jul 15, 2017

@vagrant1991 @WeRockStar You both had different solutions for the problem. @WeRockStar - you had somebody verify your commit (#293) and so I went with a slight variation of yours for now.

I didn't want to hardcode the aspect tolerance to 0.5 like you did, but wanted it to be customizable. For example take a look at the setAspectTolerance call in this example: https://github.com/dm77/barcodescanner/blob/master/zxing-sample/src/main/java/me/dm7/barcodescanner/zxing/sample/ScalingScannerActivity.java#L35

Note that this is just a temporary fix. I still don't have a good grasp of what the issue is. My questions to any of you experiencing issues on Huawei devices:

1.) Do you always see this error: "Exception: Camera new cameraInitNormal: 0" ?
2.) Are you able to replicate the error with zxing-sample app?
3.) If you don't see an exception, what do you guys mean when you say "it won't work"? Is the screen blank or is the viewfinderview too small?
4.) Is it possible to tell if this is due to the usage of deprecated Camera APIs (Camera vs Camera2)?
5.) Do you see the same error message when running the Barcode scanner app from Zxing (https://play.google.com/store/apps/details?id=com.google.zxing.client.android&hl=en)?

This temporary fix is in v1.9.4

May be I should buy myself a Huawei device to test this out.

@WeRockStar
Copy link
Contributor Author

WeRockStar commented Jul 15, 2017

@dm77 I agree with you. Shouldn’t hardcode the aspect tolerance. But my commit #293 just workaround solution for me. Thanks for support the problem.

Regards

@anastaciakv
Copy link

@dm77 You saved my day! Thank you very much for the aspect tolerance!

@sg552
Copy link

sg552 commented Sep 6, 2017

thanks, solved my problem about HUAWEI P10.

@vagrant1991
Copy link

@WeRockStar @dm77 @vickyleu @danielcerny @aegamesi @sg552 The point is not ASPECT_TOLERANCE size, the point is getOptimalPreviewSize method can't achieve the desired result on different devices. maybe huawei p9 p8 p10 p... other device.....

There is no exception, I can reproduce always, "it won't work" mean the viewfinderview is blurred, because onPreviewFrame method at ZXingScannerView can't get the right camera size. It's irrelevant to Camera API (Camera vs Camera2)

change the method to :

private Camera.Size getOptimalPreviewSize() {
    if (mCameraWrapper == null) {
        return null;
    }
    List<Camera.Size> sizes = mCameraWrapper.mCamera.getParameters().getSupportedPreviewSizes();
    int w = getWidth();
    int h = getHeight();
    if (DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {
        int portraitWidth = h;
        h = w;
        w = portraitWidth;
    }

    if (sizes == null) return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;
    Collections.sort(sizes, new Comparator<Camera.Size>() {
        @Override
        public int compare(Camera.Size lhs, Camera.Size rhs) {
            return lhs.height - rhs.height;
        }
    });

    int orientation = DisplayUtils.getScreenOrientation(getContext());
    int width;
    int height;
    if(mSquareViewFinder) {
        if(orientation != Configuration.ORIENTATION_PORTRAIT) {
            height = (int) (getHeight() * DEFAULT_SQUARE_DIMENSION_RATIO);
            width = height;
        } else {
            width = (int) (getWidth() * DEFAULT_SQUARE_DIMENSION_RATIO);
            height = width;
        }
    } else {
        if(orientation != Configuration.ORIENTATION_PORTRAIT) {
            height = (int) (getHeight() * LANDSCAPE_HEIGHT_RATIO);
            width = (int) (LANDSCAPE_WIDTH_HEIGHT_RATIO * height);
        } else {
            width = (int) (getWidth() * PORTRAIT_WIDTH_RATIO);
            height = (int) (PORTRAIT_WIDTH_HEIGHT_RATIO * width);
        }
    }

    if(width > getWidth()) {
        width = getWidth() - MIN_DIMENSION_DIFF;
    }
    if(height > getHeight()) {
        height = getHeight() - MIN_DIMENSION_DIFF;
    }

    for (Camera.Size size : sizes) {
        if (size.height > height && size.width > width) {
            optimalSize = size;
            break;
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

Now , all devices are ok. Try to get the first camera size which is larger than the size of viewfinderview

@stephenruda
Copy link

@dm77 @vagrant1991
The getOptimalPreviewSize() method proposed by vagrant1991 has worked perfectly for me.

I was having poor results with v1.9.8. Swapping in the method above solved the problems I was having.

Here were the issues I was having...

  1. Setting the ASPECT_TOLERANCE is not a feasible solution for me. For example, the barcodes scan normally on some devices with the default aspect tolerance but need aspect tolerance 0.5f in order to work correctly on other devices (the HUAWEI problem). My Galaxy S4 with front facing camera would only work with the tolerance set to 0.5f. I can't carve out a giant IF statement in my code that goes through all of the known devices that need to be set to 0.5f.
  2. I previously used v1.8.2 of the library. On all of my devices, the scanning worked just fine. On the v1.9.8, I am not getting successful scans when the barcode is very clearly in the scan region. On my Galaxy S7, I would place the barcode perfectly in the scan region and nothing would happen. I would need to hold it much further back or even adjust it a little above the scan region for it to scan successfully. Something was clearly off.

The above method solved these problems and it scans as I would expect. It is also much more helpful that you don't have to specify an ASPECT_TOLERANCE. In my opinion, this should be the method used for getOptimalPreviewSize() rather than what is currently used in v1.9.8.

Hopefully others can come forward and discuss their results with this method.

ALSO, if anyone is wondering the values of the constants (such as LANDSCAPE_WIDTH_HEIGHT_RATIO) they are found in the ViewFinderView.java file.

I'd be happy to put together a pull-request @dm77 if you are interested.

Thanks!

@geetves
Copy link

geetves commented Nov 9, 2017

+1 for pull request mentioned by stephenruda

@danielggg33
Copy link

danielggg33 commented Nov 15, 2017

Hi guys,
I have tested the zxing-sample app on Huawei Mate 9 7.0, P9 and P9 Lite 6.0 , and the problems related to the blurred scan preview and difficulties to scan absolutely don't happen.
However, I tried to implement v1.9.8 following the instructions described in the readme file and yes, it happened to me too, totally blurred on every Huawei I tried.

In my case I discovered that the problem was related to the action bar. If you check the sample app it has set <item name="windowActionBar">false</item> and its own toolbar implementation in the code. But when I tried, I did it without those two aspects.

What I did to workaround this issue is to set in the manifest file the following style to my activity:

<style name="AppOverlayTheme" parent="@style/Theme.AppCompat.Light">
   <item name="windowActionBarOverlay">true</item>
</style>

I hope this can help you, please let me know if you have any problem.

@HarshaKanchina
Copy link

Hi @vagrant1991

When I try to replace the method with yours 'getOptimalPreviewSize', I'm getting the following as unresolved symbols:

mSquareViewFinder
DEFAULT_SQUARE_DIMENSION_RATIO
LANDSCAPE_HEIGHT_RATIO
LANDSCAPE_WIDTH_HEIGHT_RATIO
PORTRAIT_WIDTH_RATIO
PORTRAIT_WIDTH_HEIGHT_RATIO
MIN_DIMENSION_DIFF

Can you please help? I'm kind of new to all this.

Or, if you have an aar/jar with the above method already replaced, can you please share it? We're running into an issue for specific devices. Didn't expect this to happen, so need a solution. Thanks.

katox added a commit to leafclick/barcodescanner that referenced this issue Oct 11, 2018
@stale
Copy link

stale bot commented May 12, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label May 12, 2020
@stale stale bot closed this as completed May 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests