一个正方形屏幕上的Android相机preVIEW拉伸
我建立的Android应用程序,但该设备确实有一个方形屏幕。屏幕是320x320起,相机应用程序使用SurfaceView显示如下preVIEW:
I'm building an app for Android but The device do have a square screen. The screen is 320x320 and the camera app use the SurfaceView to show the preview as below :
mCameraView = (SurfaceView) findViewById(R.id.surface);
LayoutParams params = mCameraView.getLayoutParams();
int camera_dimension = getResources().getInteger(R.integer.camera_dimension);
params.height = camera_dimension; //320px
params.width = camera_dimension;
mCameraView.setLayoutParams(params);
mCameraViewHolder = mCameraView.getHolder();
mCameraViewHolder.addCallback(this);
mCameraViewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
在表面改变,我做这个和它的作品,但支持者preVIEW是W480点¯xH320
On Surface changed, I'm doing this and it works but the supporter preview is w480 x h320
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
int mWidth = w;
int mHeight = h;
int mFormat = format;
try {
mCameraAccess.mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
Camera.Parameters param = mCameraAccess.mCamera.getParameters();
List<Camera.Size> SupportedPreview = param.getSupportedPreviewSizes();
int ScreenSize = getResources().getInteger(R.integer.camera_dimension);
for (int i = 0; i < SupportedPreview.size(); i++) {
if (SupportedPreview.get(i).height == ScreenSize) {
param.setPreviewSize(SupportedPreview.get(i).width, SupportedPreview.get(i).height);
break;
}
}
mCameraAccess.mCamera.setParameters(param);
mCameraAccess.mCamera.startPreview();
}
如何才能确保viewholder里面我的preVIEW不是COM pressed而是一种centercrop的。由于preVIEW是长方形的,我只需要在图像上居中的广场。通常我使用scaleType,但它不是在表面观支持
How can make sure that my preview inside the viewholder is not compressed but a kind of centercrop. As the preview is a rectangle, I just need the square centered on the image. Usually I'm using scaleType but it's not supported in Surface view
任何想法?
我已经想通了,解决的办法是:
The solution which i have figured out is:
1) - 保持表面观全屏,所以这是不舒展。
1)-Keep surface view full screen,so that is doesn't stretch.
2)在-put全opacity.So surfaceview一种观点认为是看起来像相机已经平方。
2)-Put a view over surfaceview with full opacity.So that is looks like camera is already squared.
3) - 后捕捉图像或视频,你将不得不裁剪他们。
3)-After capturing image or video,you will have to crop them.
有关视频,你必须使用一些视频处理库像javacv.Using这个库,你可以提取视频帧,将它们转换为位图,裁剪位图广场,然后重新连接code到视频。
For video you have to use some video processing library like javacv.Using this library you can extract video frames,convert them to bitmap,crop bitmap in square and then re-encode into video.
要得到你需要玩不同的技术,比如根据自己的需要拍摄等过程中变焦相机准确的结果。
To get accurate results you will need to play around with different techniques like zooming camera during capture etc. according to your needs.
原始图像:
平方图片: