java - 只获取相机的顶部

标签 java android android-camera

几天以来,我一直被困在这个问题上。我想要的只是要在屏幕上显示的相机的一部分。这就是我想要的:enter image description here

这是我所做的:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="200dp">

    <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            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>


    <TextView
            android:id="@+id/scanText"
            android:text="Scanning..."
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
    </TextView>
</LinearLayout>



@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final LayoutInflater inflator = getLayoutInflater();
        final LinearLayout inflate = (LinearLayout) inflator.inflate(R.layout.main, null);

        final SurfaceView surfaceView = (SurfaceView) inflate.findViewById(R.id.surfaceViewBeautyCamera);
        final SurfaceHolder holder = surfaceView.getHolder();

        WindowManager mW = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        int screenWidth = mW.getDefaultDisplay().getWidth();
        int screenHeight = mW.getDefaultDisplay().getHeight();

        final ViewGroup.LayoutParams slayout = surfaceView.getLayoutParams();
        slayout.width = screenWidth;
        slayout.height = screenHeight;
        surfaceView.requestLayout();

        final ScrollView beautyContent = (ScrollView) inflate.findViewById(R.id.scrollView);

        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    camera = getCamera();
                    camera.setPreviewDisplay(holder);
                    camera.setDisplayOrientation(90);
                    camera.startPreview();
                } catch (IOException e) {
                    Log.i("Lucy", e.getMessage());
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }
        });

        addContentView(inflate, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }
public Camera getCamera() {
    try {
        final Camera open = Camera.open();
        return open;
    } catch (Exception ex) {
        Log.i("Lucy", ex.getMessage());
        return null;
    }
}

当我这样做时,我得到: enter image description here

想法是将 SurfaceView 的大小设置为全屏,但减小 ScrollView 的大小。但我想要的是第一个图像。我做错了什么?

观察:

当我完全向上 ScrollView 时。返回主屏幕并再次打开应用程序。我得到第一张图片。但是,如果我再次滚动并返回主页面返回,我会得到第二张图片。

还有其他策略吗?

附言: 1. 相机顶部有其他 View 也是选项,但由于冗长的原因我不能使用它

最佳答案

问题是元素的高度设置: 您的根元素应该是一个相对布局,然后您可以设置相机 View 的固定高度,并将带有文本的 TextView 设置为与父元素高度匹配,并将 layout_below 设置为 ScrollView 。

关于java - 只获取相机的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660182/

相关文章:

Android MapView POI缓存策略

android - 获取隐式 Intent 打开的应用程序

java - 将日历日期转换为字符串?

android - 无效的命令行参数 : partition-size

java - 日期时间解析异常 : Text could not be parsed at index 2

java - Android - 从没有扩展名的文件中获取 MIME 类型

android - 在 android 上从摄像头进行 OpenCV 处理和写入视频的最快方法?

android - 直接在 Activity 中打开相机而不点击按钮并且没有 Intent ?

java - 如何从 Retrofit onResponse() 更新 Activity/Fragment UI?

java - Vaadin组件由于某种原因添加了两次