是否可以裁剪相机预览?

And*_*son 14 android android-camera

我没有找到任何方法来裁剪相机ppreview,然后在SurfaceView上显示它.

Android - 可以裁剪相机预览吗?

mom*_*omo 16

您可以在没有叠加视图的情况下执行此操作(这在所有情况下都不起作用).

子类ViewGroup,将SurfaceView添加为唯一的子项,然后:

  1. 在onMeasure中提供您想要的裁剪尺寸.
  2. 在onLayout中,使用未裁剪的尺寸布局SurfaceView.

基本上,

public class CroppedCameraPreview extends ViewGroup {
  private SurfaceView cameraPreview;
  public CroppedCameraPreview( Context context ) {
    super( context );
    // i'd probably create and add the SurfaceView here, but it doesn't matter
  }
  @Override
  protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
    setMeasuredDimension( croppedWidth, croppedHeight );
  }
  @Override
  protected void onLayout( boolean changed, int l, int t, int r, int b ) {
    if ( cameraPreview != null ) {
      cameraPreview.layout( 0, 0, actualPreviewWidth, actualPreviewHeight );
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


ste*_*ick 9

您可以将相机预览(SurfaceView)放在ScrollView内的LinearLayout中.当摄像机输出大于您设置的LinearLayout时,您可以通过编程方式滚动它并禁用用户滚动.这样您就可以轻松地模拟相机裁剪:

<ScrollView 
                     android:id="@+id/scrollView"
                     android:layout_width="640dip"
                     android:layout_height="282dip"
                     android:scrollbars="none"
                     android:fillViewport="true">

                        <LinearLayout
                                android:id="@+id/linearLayoutBeautyContent"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:orientation="vertical">

                                <SurfaceView
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:id="@+id/surfaceViewBeautyCamera"/>
                      </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的方法.但是,SurfaceView的高度是否与相机的纵横比相匹配并且大于周围的布局?使用match_parent将使其填充ScrollView的固定高度,从而产生紧凑的预览. (2认同)

Kon*_*uda 6

不是直接的.Camera API现在允许偏移,并将图像挤压到表面支架.但是你可以通过在其上放置叠加(其他视图)来解决.


归档时间:

查看次数:

20807 次

最近记录:

6 年,8 月 前