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);
}
}
}
Related
I am making a simple platformer in Unity. This is what it looks like in the editor:
You see, it is set to be in landscape in a 16 by 9 ratio. This is just fine.
Now, a few days ago, I updated my JDK to the newest version. Before, my game looked just like that on my Samsung S8. It was centered with black pillars to the left and right of it, since the screen isn't 16:9. This was great.
Now after updating, this is what it now looks like:
I am not sure why Unity does this.
Clearly, the game is now cut off on the top and lower ends so that it keeps its ratio. But this isn't what I want. I want my black pillars back to have everything be on screen and not partly be off screen.
I solved this problem with next steps:
PlayerSettings/Aspect Ratio Mode = Legasy(1.86)
PlayerSettings/Supported Aspect Ratios = 16:9 only
Canvas Scaler/UI Scale Mode = Scale With Screen Size
Canvas Scaler/Reference Resolution = any resolution for aspect ratio = 16:9 (1920; 1080)
Canvas Scaler/Match = 1 (for scale based on height)
All Cameras/Background = full black
Fix the resolution and aspect ratio issues directly: add a canvas scaler to the root canvas gameobject.
Canvas Scaler
Basically, force fixed width and let the height vary based on aspect ratio. Make sure all UI is properly anchored from the top or bottom to handle that variation.
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.
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.
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);
My Android app (a text-based game) uses background images a lot in order to provide a better visual ambiance. For example, if the action in the game takes you into a tavern, then you get a background image of a tavern in the game. This is a vast improvement, graphically, over the boring black background that you would otherwise get.
It is also a problem, however, since android:background always stretches to the dimensions of the screen. The result is that the background images look very bad if the player switches between portrait and landscape mode. And to make matters worse, many devices have very different aspect ratios (e.g., 320x480 mdpi, 480x800 hdpi, and 480x852 hdpi) and even more variations are coming.
How do others solve this problem? Having individual images for the main resolutions/orientations is not an option for me, as this would result in the apk growing too large.
The first step is to get the screen height and width of the device itself, then stretch/shrink or pad your bitmap as needed. This SO answer has source that should help, copied below. Props to Josef for original answer.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Here's generic source on getting the orientation.
int orientation = display.getOrientation();
or some more involved source from here (a little messy but looks correct).
public int getscrOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
// Sometimes you may get undefined orientation Value is 0
// simple logic solves the problem compare the screen
// X,Y Co-ordinates and determine the Orientation in such cases
if(orientation==Configuration.ORIENTATION_UNDEFINED){
Configuration config = getResources().getConfiguration();
orientation = config.orientation;
if(orientation==Configuration.ORIENTATION_UNDEFINED){
//if height and widht of screen are equal then
// it is square orientation
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
}else{ //if widht is less than height than it is portrait
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else{ // if it is not any of the above it will defineitly be landscape
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
}
}
return orientation; // return value 1 is portrait and 2 is Landscape Mode
}
I would suggest you get the width and height of your current view. Right now I am not sure what functions will give you these values but I am sure it is possible. With that you can then calculate an aspect ratio. I'd make the images something like 1000x1000 and just show a certain part of the image, so that the aspect ratio will not be wrong.
I have the same problem with the camera. So my solution was to pick one of the two values, width or height, as fixed, and calculate the correct other value according to the aspect ratio. I'm not sure if there are functions already to just show a part of the image, but I am sure you could write something to copy just a part of the image and show that part.
The drawback is of course that you will be showing just a part of the background. Since the general aspect ratio of the phones is however somewhere between 1:1.3 and 1:1.8 I guess it wouldn't be a too big problem. I for one would rather see the part of an image in the correct way, than looking at an ugly stretched image.
Maybe you could create your background images as NinePatch images so that you are in better control of how it stretches?
Otherwise, you can just prevent your images from stretching (or at least keep the correct aspect ratio) by setting the scaleType attribute e.g. android:scaleType="center".