Full screen preview Android camera2 [duplicate] - android

I'm trying to modify Google's camera2Basic example code. I removed the <FrameLayout/> containing the "Picture" and "Info" button in an attempt to make the <TextureView/> full screen. However, the preview does not fill the entire screen, there remains a black bar below it. I believe this has something to do with the AutoFitTextureView that it ships with but since they haven't provided any documentation on how it works I am unable to make modifications to it.

I noticed this exact same issue before on my Galaxy Note 5 and I believe it had to do with the way they set the aspect ratio - there are apparently some limitations with this API (or just poorly documented). I fixed it by not setting the aspect ratio on the AutoFitTextureView.
Specifically in this example, in the method setCameraOutput(int width, int height), simply remove these lines of code (lines 574 - 580 in your example):
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mTextureView.setAspectRatio(mPreviewSize.getWidth(),mPreviewSize.getHeight());
} else {
mTextureView.setAspectRatio(mPreviewSize.getHeight(),mPreviewSize.getWidth());
}
I believe that in the example they are trying to limit the capture area which likely leads to the black bar you are seeing (probably because you are building on a larger device than the one the person who developed used).

Related

Full screen preview camera2Basic Example Project

I'm trying to modify Google's camera2Basic example code. I removed the <FrameLayout/> containing the "Picture" and "Info" button in an attempt to make the <TextureView/> full screen. However, the preview does not fill the entire screen, there remains a black bar below it. I believe this has something to do with the AutoFitTextureView that it ships with but since they haven't provided any documentation on how it works I am unable to make modifications to it.
I noticed this exact same issue before on my Galaxy Note 5 and I believe it had to do with the way they set the aspect ratio - there are apparently some limitations with this API (or just poorly documented). I fixed it by not setting the aspect ratio on the AutoFitTextureView.
Specifically in this example, in the method setCameraOutput(int width, int height), simply remove these lines of code (lines 574 - 580 in your example):
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mTextureView.setAspectRatio(mPreviewSize.getWidth(),mPreviewSize.getHeight());
} else {
mTextureView.setAspectRatio(mPreviewSize.getHeight(),mPreviewSize.getWidth());
}
I believe that in the example they are trying to limit the capture area which likely leads to the black bar you are seeing (probably because you are building on a larger device than the one the person who developed used).

Image Sized a little-bit to small, and ratio changes on rotation

using the cwac-camera library,
on the left and on the right (more times just on the left), the final image and the preview gets cut after taking a picture.
Also Xperia Z1 -> FFC (Front Facing Camera) ->
left and right side images are cut
http://s7.directupload.net/images/140228/ei5saica.png
http://s14.directupload.net/images/140228/ftnecxau.png
Another issue:
Also I added the library's view into a fullscreen fragment which will not be recreated *on rotation* -> result -> somehow the screen-preview-ratio changes on
- portrait
- landscape
- and taking picture
Means: On portrait the head is a littlebit wider (more width) than on landscape, and after taking picture the preview image is also a little bit more stretched then the (live) preview berfore. But finally the image seems to be the correct one.
This Problem was seen on an HTC One :/
Note that StackOverflow is for programming questions. Nothing here is a question. If you feel that you have identified bugs, post an issue with complete steps to reproduce the problem, including device model(s) and Android OS version(s). Use StackOverflow for questions about the library.
using the cwac-camera library, on the left and on the right (more times just on the left), the final image and the preview gets cut after taking a picture.
I would guess that it is a variation on this issue.
Another issue
Part of this is presumably the same issue. With respect to "the screen-preview-ratio changes on portrait/landscape", if you are not creating a new CameraView, and you have the camera locked to landscape, then I do not see where I would be changing the preview behavior on an orientation change.
You may set preview size and picture size independently. But to achieve the best results, choose picture size that has same aspect ratio as your preview frame. E.g. for preview 640x480, you can use 3264x2448 picture. But if you use 1280x720 preview, you will be better served by a wide picture, e.g. 4128X2322.
Note that usually, the wide (16:9) format is achieved by essentially cropping a 4:3 picture.

Preview size wtith CWAC-Camera

I've been fighting for a while with https://github.com/commonsguy/cwac-camera
Managed to create my custom CameraFragment and a custom custom CameraHost.
I've overrode getPictureSize and getPreviewSize. With those two methods I'm able to find out which Sizes are able to be used. My overall idea is to capture a square image, and also preview it before capturing.
I've been testing and no device is returning a square-formatted Size, so I guess the best approach would be taking the Size nearest to my needs. Like if I need an 800x800 squared image, I will return the nearest Size, that would be (for example) 1280x1024
So far so good. The issue is the following:
Documentation states:
Usually, your CameraHost will be called with getPreviewSize()
Damn; my Nexus5 device never triggers this method, but a Genymotion emulator does. So in my emulator I can create a custom preview size, which I can't with my real device.
So I need some help with this issue. Why is it just "usually"? What can I do to change it to "always"?
On a second term, I need some tips: If I want an squared preview size, which I know it won't be available by default Sizes returned by getSupportedPreviewSizes, what can I do to display an square? Maybe overlaying a square on top of CameraFragment? Is it even possible? Or should I go for any other approach?
Anyways, congratulations on the library; it's absolutely clear and with so many options. No need to worry about "some devices". That's great as an Android Developer.
Thank you.
Why is it just "usually"? What can I do to change it to "always"?
Quoting the documentation:
If getRecordingHint() returns ANY or VIDEO_ONLY, though, CameraHost supplies the preview size via getPreferredPreviewSizeForVideo() instead of getPreviewSize(). If you wish to use a different preview size for video, return it, otherwise return null and we will use the results from getPreviewSize() instead.
On API Level 11+, the implementation of SimpleCameraHost getPreferredPreviewSizeForVideo() delegates to the getPreferredPreviewSizeForVideo() implementation on Camera.Parameters, except for devices known to return poor values for it.
what can I do to display an square? Maybe overlaying a square on top of CameraFragment?
I would try putting CameraView directly in your own UI, then layering something on top of it, using a FrameLayout or RelativeLayout to control the Z-axis ordering.

Why can't i just calculate the screen dimensions and by that resize the image?

I found a lot questions and answers of people asking how to adjust the image to fit the device screen and i thought to myself- if i can get the device resolution, using
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
why can't i just scale/resize the image by lets say 1/5 of the screen?
Thats way i can do lets say 1/5 of space 2/5 of image and another 1/5 of text and its supposed to fit all the device's, Am i missing something here?
also why do i get a "deprecated" warning in this code?
Sorry for my english and thanks in advance for the help
In Android your application may not cover entire screen. Even if you disable titlebar/statusbar etc you can still have device-specific chrome (like on screen button in Android 3.0).
why can't i just scale/resize the image by lets say 1/5 of the screen?
You can, and if you want to have your image take up some relative amount of the screen size, then this and using a LinearLayout with weights (as pointed out by others) are probably your only options. Do note that you probably don't want to do any image scaling on the main UI thread and that you should try doing it as efficiently as possible. Have a look at the guidelines for some good practices.
Also note that the screen width (and especially the screen height) isn't necessarily the amount of space available for your image and/or UI. There may be margins/padding enforced, or other components present; for instance the ActionBar.
also why do i get a "deprecated" warning in this code?
See the documentation for either Display.getHeight() or Display.getWidth():
This method was deprecated in API level 13. Use getSize(Point) instead.
Basically Google has told developers it may stop supporting the aforementioned methods in future platform versions. Hence, you should migrate away from them when possible. Most developers will end up writing their own helper method that uses the deprecated approach on older platforms (pre API level 13 in this case) and getSize(...) on newer platforms. You can use the Build.VERSION_CODES for this purpose.

Android: correct way to get screen dimensions for target orientation?

My application is bitmap intensive, with pixel-exact layout (it's a sort of game, actually, and it's pretty hard to avoid this pixel-based coordinates).
What I wanted to do is to perform some layout calculations and bitmap pre-scaling in my onCrete - I use well known API - getWindowManager().getDefaultDisplay().getSize() - to retrieve the screen size and do my calculations.
However, I've just hit an unexpected problem. My activity is configured as landscape only, but if I start my application on emulator and onCreate() is called while the emulator is locked, the screen size returned by getSize() indicates portrait orientation. Once I unlock the screen, onCreate() is called again, this time correctly in line with expected landscape mode dimensions.
I'm not sure how to handle this situation. I see the following options:
for each onCreate() call perform full layout calculation and resource scaling again. This is the logically correct solution, but I don't want to the same work twice, just to throw away the first result.
if onCreate() is called for portrait mode, just do nothing, and set black background (I can see there's a silly rotate animation when I unlock the screen, so this would become pretty much a fade-in animation)
Actually I'd prefer second option, but I'm slightly afraid of any side-effects. Anyone faced this problem?
Update (2012-07-08):
I've probably assigned a slightly misleading title to this question (sorry!), as the problem is not in retrieving the dimensions itself, nor calculating the layout. It's more about the activity being first created in portrait mode, and then recreated in landscape mode again, despite being declared as landscape-only. I initially expected (reasonably, huh?) the activity to be created in landscape orientation only.
I eventually decided to fill the activity with black color when it's created in portrait mode, no side effects observed. On Android 4.0 I can see actual rotation animation when I unlock the screen - a bit strange, but well, I guess it is supposed to inform the user that she should rotate the phone. Given that in portrait mode I just fill the screen with black color, this animation looks sort of like a fade-in and rotation combined - perfectly acceptable.
Use that
DisplayMetrics dm=new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
Using this(Look code at down) only gives you screen size and if your views has static size they will be seen in different size on every different screen.
Display screen=getWindowManager().getDefaultDisplay().getSize();
How to use:
every screen has diffrent density. So use:
float density=dm.density;
with this density, you can set your views size like that:
(YOUR_ITEM_SIZE)*density;
also look here for additional information:
http://developer.android.com/reference/android/util/DisplayMetrics.html
if the emulator is locked , can't you assume that the user can't run anything anyway , so the app doesn't need to handle this end case ?
anyway , as bmavus wrote , use getMetrics for getting the screen size . also , if you need to change the screen orientation of the app , you can do so either in the manifest or in code.
for games , i would advice using opengl solutions , and if you don't have much time digging for it , you can use third party engines that can help you , such as andengine and libgdx.

Categories

Resources