For some reason the camera feed in my program is sideways. If I hold my finger up to the camera from the bottom, it shows up as coming from the right in the surfaceview.
I have a pretty standard implementation
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(surfaceCallback);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
I've looked around but I can't seem to find any information on this. My device is a nexus One.
EDIT: If I set the screen orientation to landscape then it works fine for some reason... Can't get it in portrait though.
The orientation of the preview display image is controlled by Camera.setDisplayOrientation(int) method. For my Evo, setting the orientation to 90 makes the preview display correct for portrait mode.
...
camera.setParameters(parameters);
camera.setDisplayOrientation((orientation+90)%360);
camera.startPreview();
...
It seems that changing the orientation after the preview is started causes an exception. If you change rotate the phone/device, the SurfaceHolder.surfaceChanged callback will be called again, if you do the setDisplayOrientation and startPreview in that callback, you get an exception. I'm, going to try stopping preview, then changing the orientation then starting it again.
Note: There is also the Camera.Parameters.setRotation(int) method, that (I think) changes the orientation of the image when it is written into the (JPEG) output image when a picture is taken. This method does not change the preview display orientation.
use this line
mCamera.setDisplayOrientation(90);
in surfaceCreated method after
mCarmera.open();
Related
I've spent the last two days browsing SO to find answers, unsuccessfully.
I'm trying to reproduce a Snapchat-like app.
What I have so far
A ViewPager composed of 3 fragments.
The middle fragment is supposed to display a camera preview, just like Snapchat, on the whole surface (not truly fullscreen but it should cover the entire UI between the status bar and the bottoms buttons)
I'm able to display either:
The camera preview, with the correct ratio, but not covering its parent View (see picture 1 below)
The camera preview, covering its parent View but stretched (wrong ratio)
What I need
Avoid stretching the preview. Possibly, the preview would be cropped on both sides (left and right). See picture 2 below. I guess this is what Snapchat does. Or is there any other way?
What I went through
http://www.shakedos.com/2015/Aug/26/writing-an-android-portrait-camera-app.html
Make a SurfaceView larger than the screen (Fitting a camera preview to a SurfaceView larger than the display)
Android full screen camera - while keeping the camera selected ratio : can't find the whole code
Requirements
I need to use the Camera API version 1, not version 2.
Final photos must be taken in 4/3 format, not 16/9.
Portrait is forced although pictures can be in landscape or portrait
Thank you
I used to have a nice little fragment that loaded the camera in a frame (An image overlayed on top in a frame layout). After doing some refactoring to my code( which I have gone over Incessantly), I suddenly noticed that my camera didn't work.The SurfaceView that was supposed to show the camera was blank.
Over the last four hours, I added null checks and breakpoints everywhere. I could not find the source of the breakage. So then I swapped out most of my code for a line by line copy of the CommonsWare Camera example. The main difference is that my version was in a Fragment instead of an activity.
I understood most(if not all) of it while re-implementing it, and made it fit my current fragment based system. While it did not fix my bug, after a while I discovered that moving my SurfaceView above my ImageView worked. However, it did not provide the intended overlay effect as the frame was now effectively over the surface.
...Until I found that I had set the fragment container to a Hardware layer and forgot to return it to normal.
Beware this line:
findViewById(R.id.fragment_container).setLayerType(View.LAYER_TYPE_HARDWARE, null);
It messes with camera rendering.
Hopefully this helps some other poor soul.
The app I'm writing requires camera functionality.
So to learn about how to operate the camera, I followed this script:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
I have put the activity in my manifest, set the screen orientation for it on landscape mode.
The problem I'm having is, when the camera is held sideways (so I hold my Galaxy Tab P1000 in landscape position) the view is stretched out.
To be more specific about my script, I used an exact copy of code that Google made. It can be found in the android-sdk\samples\android-8\ApiDemos\src\com\example\android\apis\graphics\
The file itself is called CameraPreview.
I really have no clue why the screen looks so stretched. Of course, the format is weird and not square, but still, when using the default camera app installed on the device, it doesn't deform at all. This camera deforms the image when I hold it sideways and move the camera even a little.
What I did was: I held my galaxy tab to take a picture of an object (laptop in this case) then took a picture with my phone of my Galaxy. On the Galaxy I have the camera screen open in the app i'm making. This counts for both images. One I hold sideways and one I hold in portrait view. The pics are a bit unclear but you can see that in the landscape picture, the camera has become massively wide.
I faced the same problem yesterday. After a researching "Camera" sources I found a reason for camera preview being stretched.
The reason is: SurfaceView aspect ratio (width/height) MUST be same as Camera.Size aspect ratio used in preview parameters. And if aspect ratio is not the same, you've got stretched image.
So, the fastest workaround is to set SurfaceView to size like 320px x 240px - smallest supported size from Parameters.getSupportedPreviewSizes().
Also, you can look at Camera standard application sources, it uses the custom layout for controlling the SurfaceView size (see PreviewFrameLayout.java, onMeasure() function).
Use
git clone https://android.googlesource.com/platform/packages/apps/Camera.git
to get Camera sources.
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
camera = Camera.open();
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(holder);
Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPictureSizes();
parameters.setPictureSize(sizes.get(0).width, sizes.get(0).height); // mac dinh solution 0
parameters.set("orientation","portrait");
//parameters.setPreviewSize(viewWidth, viewHeight);
List<Size> size = parameters.getSupportedPreviewSizes();
parameters.setPreviewSize(size.get(0).width, size.get(0).height);
camera.setParameters(parameters);
camera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You need only getSupportedPreviewSizes() and save it to a List:
List<Size> size = parameters.getSupportedPreviewSizes();
parameters.setPreviewSize(size.get(0).width, size.get(0).height);
camera.setParameters(parameters);
camera.startPreview();
I hope this helps you.
Is it possible to display an image in the background of the camera preview? The image be 80% transparent, so don't worry about not seeing the preview. I want to be able to see both the camera preview and the transparent image. I believe this falls into the augmented reality category
This answer is relevant to your interests also this
my problem.
I create app which do preview from camera on android.
For preview while im not press on button - i used Camera , camera.open. When app started - the orientation of screen is normal.. But when i try to record video - i closed camera preview and used only SurfaceView with recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
It's showing me preview from camera without class Camera.
All this i do becouse it's fixed for me bug with greenish screen after recorded.
So... this preview(which without camera) he after start always rotated on 90 degree ( Like in mode LANDSCAPE)
im try to use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
and
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
but it not fixed my problem.
So, can any one tell me how to rotate my preview(without camera) in mode like SCREEN_ORIENTATION_PORTRAIT ??
pls tell me how to fix this problem.. i can't fix it 2-3 days :(
Regards, Peter
p.s. sorry for my bad english, hope u understand me.
it's code which i used for preview when i recorded video/audio
preview = new SurfaceView(withPreview.this);
preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
get picture from camera
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
now the screen rotated on 90 degree. if i set
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
than image rotated on 180 degree О_О
and set preview
recorder.setPreviewDisplay(holder.getSurface());
that's all what i need for camera preview
How u can see im not using Camera.. so i can't us camera.parameters.setOrientation(90) //example
:]
is it right, that you will fix the Screen Orientation to Landscape or Portrait? Or should it be dynamic?
So if i understand you, to fix the orientation of the screen, then i prefer to add this into your Android Manifest:
android:screenOrientation="landscape"
This must inside the activity-block.
Hope this will help, otherwise tell me when it should dynmaic or so on.