我正在尝试开发自己的相机活动,但我有一个问题,我无法解决......
我想要的是与Instagram相框非常相似的东西,这就是我得到的:
当我得到这样的东西时:
和...
当我得到类似的东西时:
我认为我只是使用
很好地管理SurfaceView和Camera预览Camera.Parameters parameters = camera.getParameters();
camera.setDisplayOrientation(90);
和自定义SurfaceView:
public class SquaredSurfaceView extends SurfaceView {
private int width;
private int height;
public SquaredSurfaceView(Context context) {
super(context);
}
public SquaredSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquaredSurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = width;
setMeasuredDimension(width, width);
}
public int getViewWidth() {
return width;
}
public int getViewHeight() {
return height;
}
}
我做错了什么? : - (
答案 0 :(得分:18)
如前所述,您需要找到正确的预览大小(宽高比为1:1的大小),并且可能必须使用FrameLayout作为SurfacePreview。看起来你有宽高比问题可能你有正确的预览尺寸,但你把它放在一个不正确的布局。
另一个解决方案可能是(就像Instagram一样)让您的相机处于全尺寸,然后隐藏布局的某些区域,使其看起来像一个正方形。然后通过软件你必须剪切图像,使其成为一个真正的正方形。
希望这有助于你
答案 1 :(得分:6)
对我有用的解决方案是第二个答案,但因为我们需要旋转相机90º才能用HEIGTH切换WIDTH,就像这样......
camera.setDisplayOrientation(90);
Camera.Parameters params= camera.getParameters();
surfaceView.getLayoutParams().width=params.getPreviewSize().height;
surfaceView.getLayoutParams().height=params.getPreviewSize().width;
希望此解决方案有所帮助!! :d
答案 2 :(得分:2)
我的解决方案更像是构建一个方形蒙版,然后将其放在预览表面上。
主要需要3件事,首先是方框组件。我已经制作了一个自定义组件:
package com.example.squaredviewer;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Display;
import android.view.WindowManager;
import android.widget.RelativeLayout;
/**
* Created by yadirhb on 14-08-2015.
*/
public class SquaredFrame extends RelativeLayout{
public SquaredFrame(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(size, size);
}
}
根据您正在开发的Android API版本,您可能需要添加另一个构造函数重载。对于Kitkat来说,这很好。
第二步是构建布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:visibility="visible">
<RelativeLayout
android:id="@+id/camera_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_centerInParent="true"
android:background="#ffffff">
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#131008">
</LinearLayout>
<com.example.squaredviewer.SquaredFrame
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"></com.example.squaredviewer.SquaredFrame>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#131008" />
</LinearLayout>
</RelativeLayout>
注意RelativeLayout&#34; camera_preview&#34;是用于渲染预览的那个,它居中并且具有包含平方分量的LinearLayout。这实际上是&#34;掩码&#34;它涵盖了相机预览。 另请注意,除了透明的SquaredFrame外,其他两个背景颜色为黑色。
现在是表面视图,用于相机预览,其中表面根据纵横比调整大小。
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private final String TAG = "PIC-FRAME";
private SurfaceHolder mHolder;
private Camera mCamera;
private Display display;
public CameraPreview(Activity context, Camera camera) {
super(context);
mCamera = camera;
display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setKeepScreenOn(true);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
this.getHolder().removeCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
try {
// stop preview before making changes
mCamera.stopPreview();
// set preview size and make any resize, rotate or
// reformatting changes here
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = parametes.getSupportedPreviewSizes().get(0);
parameters.setPreviewSize(previewSize.width, previewSize.height);
// start preview with new settings
mCamera.setParameters(parameters);
// Set the holder size based on the aspect ratio
int size = Math.min(display.getWidth(), display.getHeight());
double ratio = (double) previewSize.width / previewSize.height;
mHolder.setFixedSize((int)(size * ratio), size);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
}
现在一切都必须绑定在活动类
中@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture_taker);
mDecorView = getWindow().getDecorView();
//mCamera = a camera instance;
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
//Layout where camera preview is shown.
RelativeLayout preview = (RelativeLayout) findViewById(R.id.camera_preview);
//FrameLayout stack controllers inside and superpose them.
preview.addView(mPreview, 0);
// TODO
}
有点长,但我希望它不止一个。 : - )
答案 3 :(得分:2)
我在方形视图中遇到了同样的问题 - 只需将SurfaceView的相机尺寸设置为我想要绘制它的视图大小即可解决它。没有复杂的计算,它对我有用。请参阅我在此处对整个方法的回答:https://stackoverflow.com/a/39430615/5181489
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
...
params.setPreviewSize(viewParams.height, viewParams.width);
camera.setParameters(params);
答案 4 :(得分:-1)
您可以使用它来获取pictureSize:
public static void initialCameraPictureSize(Context context, android.hardware.Camera.Parameters parameters) {
List list = parameters.getSupportedPictureSizes();
if(list != null) {
android.hardware.Camera.Size size = null;
Iterator iterator = list.iterator();
do {
if(!iterator.hasNext())
break;
android.hardware.Camera.Size size1 = (android.hardware.Camera.Size)iterator.next();
if(Math.abs(3F * ((float)size1.width / 4F) - (float)size1.height) < 0.1F * (float)size1.width && (size == null || size1.height > size.height && size1.width < 3000))
size = size1;
} while(true);
if(size != null)
parameters.setPictureSize(size.width, size.height);
else
Log.e("CameraSettings", "No supported picture size found");
}
}
答案 5 :(得分:-1)
这太难了。不容易。
第一件事:您定义2个黑色Linear Layout
以获取您发布的用户界面。
第二件事:你需要将图片从全图片切割成方形图片。
如何剪切,您需要使用scaleBitmap
方法。
或者您想要真正的自定义相机?可以登记here