jhdj
jhdj

Reputation: 631

Foreground Camera Plugin set camera to portrait and fix stretched preview

Foreground Camera Plugin is set at landscape default how can you set the camera to portrait? I set the xml to portrait but can't seem to find how to change the camera preview orientation.

Update: Added the following to fix lanscape

Camera.Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG); 
parameters.set("orientation", "portrait");
parameters.setRotation(90);
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);

this is my current code to create a square preview

getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);  
int wwidth = displaymetrics.widthPixels;
mFrameLayout.getLayoutParams().height = wwidth;
mFrameLayout.getLayoutParams().width = wwidth;

However preview is stretched and picture seems stretched.

  1. How do I get the height required for a specific width that will not stretch the view?

  2. Can I hide the bottom excess? (Since it will be cropped out after taking the picture)

  3. Also found the the camera seems to be zoomed and the picture taken is not the same as the one you see in the preview it has more content.

Can anyone give me some tips?

Thanks

Upvotes: 0

Views: 954

Answers (1)

Jan Tchärmän
Jan Tchärmän

Reputation: 965

To fix the stretching on changing orientation, add these lines to the 'surfaceChanged' Method in your ForegroundCameraPreview.java file, BEFORE calling mCamera.setPreviewDisplay()

android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
    case Surface.ROTATION_0: degrees = 0; break;
    case Surface.ROTATION_90: degrees = 90; break;
    case Surface.ROTATION_180: degrees = 180; break;
    case Surface.ROTATION_270: degrees = 270; break;
}    
int result;
result = (info.orientation - degrees + 360) % 360;

mCamera.setDisplayOrientation(result);

You need to obtain the activity (mActivity) from the CameraActivity (e.g. pass it to the constructor of ForegroundCameraPreview and save it to private variable etc.)

Upvotes: 1

Related Questions