Barcode-Reader
Barcode-Reader copied to clipboard
camera screen is zoomed
some zoom level is set by default.. Image is expanded.

this is how it looks in paytm

Is the issue still exists?
yes @ravi8x
I'm also getting the same issue.
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
@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);
}
}
@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 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.
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 i've tried your fix..but i'm having a green stripe on the right side of the screen

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 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.
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
Hello. it's seem still not fix in new version gradle.
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
I'm also experiencing the same problem (a green bar appears on right of the scanner ). Did somebody find the fix for that?
hello @mmarshad . Let download this project and add to your Android Studio as library, then modify the class CameraSourcePreview as above comment
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
}
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 ?
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
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);
This solve my problem by setting the screen to full screen
What still not fixed this issue ??