Jatin
Jatin

Reputation: 31744

Get only top part of camera

I am stuck at this Issue since couple of days. What I want is only part of the camera to be shown on screen. This is what I want:enter image description here

This is what I have done:

<?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;
    }
}

When i do this I get: enter image description here

The Idea is to set the size of SurfaceView as full screen but reduce the size of scroll view. But what I want is the first Image. What am I doing wrong?

Observations:

When I scroll the view up completely. Go back to Home Screen and open the app again. i get the first image. But if I scroll again and go back to main to come back, i get the second Image.

Any other strategy?

PS: 1. Having other view on top of camera is also option, but I cannot use it for lengthy reasons

Upvotes: 4

Views: 324

Answers (3)

dharmendra
dharmendra

Reputation: 7881

I dont know why you put surfaceView in the scrollView But I put up with your layout file and its works for me as you show in expected snapshot result .

Edits:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:fillViewport="true"
            android:scrollbars="none" >

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

                <SurfaceView
                    android:id="@+id/surfaceViewBeautyCamera"
                    android:layout_width="match_parent"
                    android:layout_height="160dp" />
                 <TextView
                     android:id="@+id/scanText"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:layout_alignParentBottom="true"
                     android:text="Scanning..." >
                 </TextView>
            </LinearLayout>
      </ScrollView>
   </RelativeLayout>
</LinearLayout>

Upvotes: 1

Koushik
Koushik

Reputation: 352

try this layout.But don't understand why use scrollView.

I post 2 layout

First one where surface mean camera face is 160dp

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

    <SurfaceView
        android:id="@+id/surfaceViewBeautyCamera"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>



    <TextView
        android:id="@+id/scanText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         android:layout_alignParentBottom="true"
        android:layout_below="@id/linearLayoutBeautyContent"
        android:background="#000000"
        android:text="Scanning..." >
    </TextView>

    </RelativeLayout>

2nd one is, camera will capture fullscreen size picture but u can see only 160 window at time of capture

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

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

    <SurfaceView
        android:id="@+id/surfaceViewBeautyCamera"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

<TextView
    android:id="@+id/scanText"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="160dp"
    android:background="#000000"
    android:text="Scanning..." >
</TextView>

</RelativeLayout>

Upvotes: 1

Display name
Display name

Reputation: 2747

The problem is the height settings for your elements: Your root element should be a relative layout, you can then set the fixed height of the camera view and set the textview with the text to height match parent and layout_below the scroll view.

Upvotes: 1

Related Questions