I'm creating a SurfaceView for my camera preview and attempting to have it occupy a rectangle that's smaller than the entire screen, but it's ignoring my layout command.
I assume I'm doing something wrong, but can't find an example of someone setting the preview into a smaller container.
All advice (or pointers to examples) appreciated.
That's how it's done in the default Camera application: layout file and source code.
There's a container that performs a special layout procedure depending on a given aspect ratio. I think you can adapt this solution to your needs.
Related
I am using the Camera2 API to setup an imagecapture fragment. I only want to use the camera in portrait mode. So i have set this line in the manifest in the fragment's parent activity:
android:screenOrientation="portrait"
Which locks the camera preview to portrait. However, the image aspect is just wrong. When the image is captured, there is no issues with the aspect, and the image looks as it should, but is still not in full screen. I suspect the camera output is stretched to fill the screen in my preview. How do i make both the preview and the captured image fill the entire screen and keep its aspect ratio like Snapchat does it?
I have been using the Google sample as my reference, and changed the AutoFitTextureView to match_parent in the XML layout file.
this is how it looks in normal portrait
and this is how it looks when i rotate the phone and is still in portrait mode
AutoFillTextureView attempts to keep the aspect ratio correct, but if you're changing its layout parameters, it may not be able to.
Do things work correctly if you don't make it 'match_parent'?
In general, Android UI elements will fill themselves with the input data, so you need to set the View's size to the right aspect ratio to avoid stretching.
For anyone who has been facing this issue like I was and if you have been using the google sample from before Jun 2017, then tweaking a line in your AutoFitTextureView should fix the preview's aspect ratio. It's as simple as changing "<" to ">" which you can see here.
is there a way to have a small camera preview inside a view? Not the classic fullscreen preview, I mean.
My idea is to create an app that allows the user to take pics of himself with the frontal camera, when it's dark.
Basically, I thought to put a small preview of the camera on the corner of the screen and setting the remaining part to full brightness (to use it like a "self flash").
Well, I don't have problems actually to set a view to a full brightness value, but I don't have any idea how to resize the frontal camera preview.
I hope you understood my intention...
Any help would appreciated!
I don't believe this idea will hold water, because the brightness of the screen may not be enough. But the technical question you ask is not very hard: you can put the ViewSurface for your preview in the main activity's xml with any size you choose (you can use the usual WISIWYG layout editor in ADS or AndroidStudio).
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.
I have a layout where the top part is used to display some data (eg. simple UI widgets) and the bottom part has a SurfaceView where I display a CameraPreview.
In order for this SurfaceView to extend the whole width of the screen and at the same time maintain a correct aspect ratio, the SurfaceView actually goes offscreen which is fine since I actually want the app to be divided evenly between the top part and the bottom part.
The problem is that I now want to retrieve only the visible part of the SurfaceView (ideally as a bitmap) but if I try to do it by setting a PictureCallBack, the aspect ratios of the picture data returned are just not quite the same as the ones shown in the SurfaceView (I've tried with all of the available getSupportedPictureSizes).
Any ideas how to tackle this problem? I've attached an image which better illustrates this problem.
https://code.google.com/p/zxing/downloads/list
it contains a barcode scanner project in that they have used a rectangle and capture particular area from the surfaceview it will be useful for u .. Hope so they use viewFinderView class to capture the area from the surface
I am developing a camera app but the camera is not full screen. I am using common "CameraPreview" class to open camera. it includes Surfaceview and Determining Optimal Size for camera. In tablet and phones, camera is not full screen. For example, tablet size is 1280x728 but camera size is 1024x768. It is shown in FrameLayout for which layout widht and height is FillParent. I could not understand and solve this issue. I wanna open camera in full screen without stretching.
Is there anybody got this issue and fix it?
Judging by the code here:(which you can use actually), you can use the method getSupportedPreviewSizes() and see what are the supported resolutions, 1024x768 could easily be the highest one available for this preview and as you found out, it is really the highest supported.
So those are the supported resolutions, there's no way you can get higher than the supported. You must find a workaround for this, like to put the preview in the center and surrond it with black frame or something like that.