Full screen camera preview in portrait mode on Android 4.3 - android

I am using the Intel Inde library for media (specifically this example: https://github.com/INDExOS/media-for-mobile/blob/master/Android/samples/apps/src/com/intel/inde/mp/samples/CameraCapturerActivity.java)
My goal is to set up a full screen preview in portrait mode and record videos in portrait mode. Everything works great in landscape mode(full screen preview and recording). However in portrait mode I can't stretch the preview to fill the screen vertically (even though I set the surface view to max size). The largest size I get is a square image preview. I have tried making the surface view larger then the screen, but still stuck with a square preview and recording.
This is what my preview screen currently looks like:
http://imgur.com/XUznvF1
Here is my code:
public void createCamera()
{
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> supportedResolutions = camera.getParameters().getSupportedPreviewSizes();
parameters.setPreviewSize(supportedResolutions.get(0).width,
supportedResolutions.get(0).height);
camera.setDisplayOrientation(90);
camera.setParameters(parameters);
}
public void createPreview()
{
GLSurfaceView surfaceView = new GLSurfaceView(getActivity().getApplicationContext());
//This code is used to get display size to set surface view size
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
surfaceView.setLayoutParams(new FrameLayout.LayoutParams(width, height));
//Used to test that the surfaceview does cover the whole screen
//surfaceView.setBackgroundColor(Color.RED);
((RelativeLayout)view.findViewById(R.id.myview)).addView(surfaceView, 0);
preview = capture.createPreview(surfaceView, camera);
preview.start();
}

There is a thread on the Intel Developer site, appears to be the same issue. They have a solution, but you have to request the library update via email:
https://software.intel.com/en-us/forums/topic/542633

I think your problem is that you are setting the preview size of your camera to the first size in the list of supported sizes.
List<Camera.Size> supportedResolutions = camera.getParameters().getSupportedPreviewSizes();
parameters.setPreviewSize(supportedResolutions.get(0).width, supportedResolutions.get(0).height);
What you should do is loop through the list of supported sizes and find a size that closely matches the screen's size/resolution.
You can check this SO answer for sample code snippets
Update
The next step would be to debug it and find out why it didn't work for you. What I would do in your case would be to print out the contents of the supportedResolutions list. Since you already the have the display size, I would compare the display size to the different sizes in the supportedResolutions list and also to the size returned from the getOptimalSize method. It's possible that the getOptimalSize method didn't find an optimal size and the method may need to be tweaked to your need. I tweaked mine to get it to work.
Let me know if you need more help.

Related

Changing the layout of SurfaceView on nVIdia Shield turns the screen black

I am rendering a video on a SurfaceView in my android app, and I want to resize the surface to match the video size. I do this by setting a new Layout like so:
FrameLayout.LayoutParams videoLayout = new FrameLayout.LayoutParams(width, height);
videoLayout.leftMargin = x;
videoLayout.topMargin = y;
view.setLayoutParams(videoLayout);
Seems to work fine on other android tv boxes but on nVidia Shield the screen goes black. It only works if width and height match the actual resolution, for anything other it's just black.
Also works if I render on a TextureView, but then I take a big performance hit.
Also tried with setFixedSize, same outcome.
view.getHolder().setFixedSize(width, height);

Camera2 preview stretched

On my Moto E, I want my preview to be full screen hence i have set the texture view's dimension to be full screen i.e. 540*960by overriding onMeasure, but using camera2 api the closest match of preview size that i am getting is 768*432 which is of the same aspect ratio. Google sample suggests that "Initially, output stream images from the Camera2 API will be rotated to the native device orientation from the sensor's orientation, and the TextureView will default to scaling these buffers to fill it's view bounds. If the aspect ratios and relative orientations are correct, this is fine." But even when in native orientation I am getting stretched previews. Even if I setTransform to Identity matrix, I don't know how the preview is still scaled automatically to fill the whole screen.
I also encountered this problem and solved it like this:
re-set surfaceview params (width and heigth).
ViewTreeObserver observer = mSurfaceView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
FrameLayout.LayoutParams surfaceParams = (FrameLayout.LayoutParams) mSurfaceView.getLayoutParams();
surfaceParams.height = screenHeight;
surfaceParams.width = screenWidth;
mSurfaceView.setLayoutParams(surfaceParams);
mSurfaceView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});

Custom camera Android preview picture is different from show picture

I am working on an Android app that uses the phone's camera and I'm using a custom code.
The problem is that the image is showing in the preview is not correct (the image aspect ratio is not OK).
If I use the following code:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width,selected.height);
mCamera.setParameters(params);
mCamera.startPreview();
the preview image is OK (I think little part of the image top and bottom is not showed).
The problem then is that the captured image showed after takePicture method is called is not showed correctly (the image aspect ratio is not OK, the image appears to be compressed into the view), even so if I save the picture to a file image appears to be OK.
I wonder if it is possible to show the image correctly in both cases. Suggestions?
The solution was easy, but I didn't know I must set correct aspect ratio MANUALLY (it is not an automatic process, I supposed it was and this step it is not explained in most how to's). Finally, I solved it this way:
Getting supported sizes using params.getSupportedPreviewSizes() and params.getSuppertedPictureSize().
Computing the best aspect ratio (width/height) according to the measures width and height passed in surfaceChanged(SurfaceHolder holder, int format, int width, int height).
Setting the best preview size and picture size using setPreviewSizeand setPictureSize, respectively, before startPreview() method is called.
If you saved the image and it appears correctly, the problem isn't the image being taken, it's how you're displaying it back to the user. If you're using an ImageView, make sure you're using src (not background), as src will keep the aspect ratio, but background will stretch and distort the image to fill the background. Likewise, you can set android:adjustViewBounds="true" to have the ImageView adjust its boundaries to preserve the aspect ratio.

Android Emulator Camera Preview Sizes

At runtime I use getSupportedPreviewSizes() and it returns a single size (320x240). When I initialize a new camera however, it sets the Preview size to the size of the display (320x480) which isn't listed under the returned supported sizes. Why is this? Are you just simply able to override the supported preview sizes, and that's what's done by default? Or is this emulator related? (I would think it's an emulator thing).
If you mean by PreviewSize the size of the SurfaceView, you can change this by code after you get your camera resolution as follows:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(cameraRes.x, cameraRes.y);
surfaceView.setLayoutParams(params);
If you mean the Camera preview size, you can set it as follows:
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
camera.setParameters(parameters);

How to make a resolution independent camera preview on android?

I'm making an android 1.6 app that uses phone's camera.
In order to do this app resolution independent, I need to set a compatible aspect ratio for previewing camera on a SurfaceLayout. In 1.6 sdk there is no way to get supported sizes for the camera preview. It is possible to use a 4:3 or 3:2 aspect ratio and get no errors whith that?
On the other hand, I need a way to make a xml layout that represents this Surfacelayout in this (unknown) aspect ratio in every resolution. I assume that is not possible to change the SurfaceLayout size in runtime. Can I do it with "dp" units? The other way is making this layout programmatically?
There are some apps like Vignette or android camera application with some tricks to make something like that, like black bars (vignette) or fixed buttons bar, but I don't know how to do it in any kind of resolution.
Any ideas?
Thanks!
In 1.6 sdk there is no way to get supported sizes for the camera preview.
There is. You can get them in parsing the preview-size-values camera parameter.
String supportedSizesString = parameters.get("preview-size-values");
List<Size> supportedSizes = new ArrayList<Size>();
if (supportedSizesString != null && supportedSizesString.length() > 0) {
StringSplitter ss = new TextUtils.SimpleStringSplitter(',');
ss.setString(supportedSizesString);
for (String supportedSize : ss) {
String[] dimentions = supportedSize.split("x");
if (dimentions.length > 1) {
Size size = mCamera.new Size(Integer.parseInt(dimentions[0]),
Integer.parseInt(dimentions[1]));
supportedSizes.add(size);
}
}
}

Categories

Resources