Barcode-Reader icon indicating copy to clipboard operation
Barcode-Reader copied to clipboard

camera screen is zoomed

Open Prathibha-Kirthi opened this issue 7 years ago • 22 comments

some zoom level is set by default.. Image is expanded. image

this is how it looks in paytm image

Prathibha-Kirthi avatar Nov 07 '17 12:11 Prathibha-Kirthi

Is the issue still exists?

ravi8x avatar Nov 16 '17 07:11 ravi8x

yes @ravi8x

Prathibha-Kirthi avatar Nov 20 '17 06:11 Prathibha-Kirthi

I'm also getting the same issue.

AnujProject avatar Nov 28 '17 12:11 AnujProject

I'm facing the same issue. For me it happens right after allowing the application to take picture and record video (granting permission). If the app already got the permission (re-launching the activity) it just works fine

pesutsoos avatar Dec 11 '17 13:12 pesutsoos

@prathibhaprabs: Get the source code you can custom and fix that issue. At CameraSourcePreview.java -> protected void onLayout (boolean changed, int left, int top, int right, int bottom). I fixed for my project like below: @Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) { int width = getWidth(); int height = getHeight();

// if (mCameraSource != null) { // Size size = mCameraSource.getPreviewSize(); // if (size != null) { // width = size.getWidth(); // height = size.getHeight(); // } // } // // int layoutWidth = right - left; // int layoutHeight = bottom - top; // // // Swap width and height sizes when in portrait, since it will be rotated 90 degrees // if (isPortraitMode()) { // int tmp = width; // //noinspection SuspiciousNameCombination // width = height; // height = tmp; // } // // int childWidth = layoutWidth; // int childHeight = (int) (((float) layoutWidth / (float) width) * height); // // if (isPortraitMode()) { // childHeight = layoutHeight; // childWidth = (int) (((float) layoutHeight / (float) height) * width); // }

    for (int i = 0; i < getChildCount(); ++i) {
        getChildAt(i).layout(0, 0, width, height);
    }

    try {
        startIfReady();
    } catch (SecurityException se) {
        Log.e(TAG, "Do not have permission to start the camera", se);
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    }
}

hoang08t2 avatar Dec 19 '17 08:12 hoang08t2

@ravi8x, do you think the fix proposed by @hoang08t2 can be accepted and reported to main code in a new version ? Thanks. Can you add a way to set autofocus 'on' from code, actually this can be done only if you define it in xml. Thanks for all.

Llorc avatar Dec 21 '17 08:12 Llorc

@Llorc currently, The problem will come when you use a device with high density so that's my solution to fix that issue. I hope @ravi8x will upgrade to a new version.

hoang08t2 avatar Dec 21 '17 10:12 hoang08t2

Listed solution is not correct. It will break aspect ratio. The problem here is that onLayout() of CameraSourcePreview is called before CameraSource#mPreviewSize is updated to proper value. By default mPreviewSize holds initial values (1024x768) or those set in CameraSource.Builder#setRequestedPreviewSize(int, int). So that is the cause of initial deformation.

This must be fixed by forcing onLayout() once mPreviewSize is correct. Quick fix is adding requestLayout() to CameraSourcePreview#startIfReady().

private void startIfReady() throws IOException, SecurityException {
        if (mStartRequested && mSurfaceAvailable) {
            mCameraSource.start(mSurfaceView.getHolder());
            if (mOverlay != null) {
                Size size = mCameraSource.getPreviewSize();
                int min = Math.min(size.getWidth(), size.getHeight());
                int max = Math.max(size.getWidth(), size.getHeight());
                if (isPortraitMode()) {
                    // Swap width and height sizes when in portrait, since it will be rotated by
                    // 90 degrees
                    mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
                } else {
                    mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
                }
                mOverlay.clear();
                requestLayout();
            }
            mStartRequested = false;
        }
    }

mroczis avatar Jan 02 '18 18:01 mroczis

@mroczis i've tried your fix..but i'm having a green stripe on the right side of the screen screenshot_2018-01-06-17-53-13

shankhajana avatar Jan 06 '18 12:01 shankhajana

I found a solution that solve the issue on my side:

  • first, on CameraSourcePreview#startIfReady() , move the requestLayout() juste before the last '}'
  • second, modify the CameraSourcePreview#onLayout() to adjust/center the view
@Override
    protected void onLayout (boolean changed, int left, int top, int right, int bottom)
    {
        int width = getWidth();
        int height = getHeight();

        if (mCameraSource != null) {
            Size size = mCameraSource.getPreviewSize();
            if (size != null) {
                width = size.getWidth();
                height = size.getHeight();
                // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
                if (isPortraitMode()) {
                    int tmp = width;
                    //noinspection SuspiciousNameCombination
                    width = height;
                    height = tmp;
                }
            }
        }

        int layoutWidth = right - left;
        int layoutHeight = bottom - top;


//        int childWidth = layoutWidth;
//        int childHeight = (int) (((float) layoutWidth / (float) width) * height);
//
//        if (isPortraitMode()) {
//            childHeight = layoutHeight;
//            childWidth = (int) (((float) layoutHeight / (float) height) * width);
//        }

        float ratio =  ((float)Math.max(layoutHeight, layoutWidth))/(float)Math.min(width, height);
        int childWidth = (int) ((float) width * ratio);
        int childHeight = (int) ((float) height * ratio);
        int childLeft = (childWidth>layoutWidth)?(childWidth-layoutWidth)/-2:0;
        int childTop = (childHeight>layoutHeight)?(childHeight-layoutHeight)/-2:0;


        for (int i = 0; i < getChildCount(); ++i) {
            getChildAt(i).layout(childLeft, childTop, childWidth, childHeight);
        }

        try {
            startIfReady();
        } catch (SecurityException se) {
            Log.e(TAG, "Do not have permission to start the camera", se);
        } catch (IOException e) {
            Log.e(TAG, "Could not start camera source.", e);
        }
    }

I hope this will solve the issue for you too ;)

Llorc avatar Jan 15 '18 11:01 Llorc

@Llorc your solution did not work for me. At first, it seemed fine when I tried, but if you put the app to background and then come back, a black stripe appears on the right.

alanpaivaa avatar Jan 18 '18 17:01 alanpaivaa

Hi @Llorc ,

Your solution works but I can't get it scanned. The scan functionality was somehow didn't went through the scanning process.

Nothing happens when I tried scanning the barcode/qrcode. Are you experiencing the same? How can I get the scanning works? I'm running it on Amazon KF tablet 5th generation.

Any help would be greatly appreciated. Thank you!

Cheers, Christian

ccsavvy avatar Jan 30 '18 11:01 ccsavvy

Hello. it's seem still not fix in new version gradle.

fukemy avatar Apr 13 '18 07:04 fukemy

Also experiencing a green bar on the scanner, using this library on a custom app on the following device : https://fr.aliexpress.com/item/IPDA020-Tablet-pos-terminal-Android5-1-System-Wirelss-portable-bluetooth-58mm-thermal-printer-PDA-Sunmi-V1/32827863258.html?spm=a2g0s.9042311.0.0.AHhM7K

Renrhaf avatar Apr 24 '18 22:04 Renrhaf

I'm also experiencing the same problem (a green bar appears on right of the scanner ). Did somebody find the fix for that?

mmarshad avatar Jun 27 '18 15:06 mmarshad

hello @mmarshad . Let download this project and add to your Android Studio as library, then modify the class CameraSourcePreview as above comment

fukemy avatar Jun 28 '18 12:06 fukemy

In my case the green line only shows if i don't use Full Screen, Below is what i did to fix it. Hope it helps others.

In Android Manifest.

 <activity android:name=".ScannerActivity"
        android:theme="@style/FullTheme"
        android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.Scanner" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
   </activity>

Style used.

<style name="FullTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Activity's OnCreate

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    
    /* use as full screen */
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN)

    setContentView(R.layout.activity_scanner)

    /* hide keyboard if necessary */
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(relativeScanner.windowToken, 0)

    //# Initialize and show barcode fragment
    mBarcodeReader = supportFragmentManager.findFragmentById(R.id.barcode_fragment) as BarcodeReader

}

johnguild avatar Jun 30 '18 17:06 johnguild

As I am adding the barcode scanner from a fragment, setting full-size will not work for me. Why does it take so long to fix this ?

1amGr00t avatar Jul 03 '18 11:07 1amGr00t

Any ideas about this issue?. I am trying fix this but when I apply style full Theme the Qrs are detected in the right side not in the center. Any solution?

Thansk

moisesvs avatar Feb 15 '19 11:02 moisesvs

I also had this issue (camera screen is zoomed). I solved this problem just added full screen mode in onCreate method:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

AlekseyDanilov avatar May 08 '19 09:05 AlekseyDanilov

This solve my problem by setting the screen to full screen

originalkwame avatar Mar 18 '20 22:03 originalkwame

What still not fixed this issue ??

sanjeet007 avatar Jun 29 '21 15:06 sanjeet007