Java – get only the top of the camera
A few days later, I got into this problem. All I wanted was a part of the camera displayed on the screen. That's what I wanted:
That's what I did:
<?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:
The idea is to set the size of the surfaceview to full screen, but reduce the size of the scrolling view. But what I want is the first picture. What did I do wrong?
Observation:
When I scroll the view completely. Return to the home screen and open the application again. I get the first picture. But if I scroll back and return to the main view again, I get the second picture
Are there any other strategies?
PS: 1. Other views on the top of the camera are also options, but I can't use it for a long time
resolvent:
The problem is the height setting of the element: your root element should be a relative layout. Then you can set the fixed height of the camera view and set the textview to match the height of the parent's text view and layout_ Below scrolling view