0

I used this library https://github.com/pedrovgs/DraggablePanel and I have a youtube fragment at top side, a camera preview (inside a fragment) at bottom side.

I want my camera preview show only half bottom part of camera (I don't take pictures, I just preview the bottom part of the front camera is like a mirror) How do I crop my camera preview? Thank you all the help!

I've tried set SurfaceView height=screenwidth and parentView height=my_height but my top fragment (youtube fragment) was displayed like a SurfaceView

Cver
  • 73
  • 11

3 Answers3

0

set fix height (e.g. 200dp)to surfaceView and align it to the bottom of the parent(screen). and set the camera preview size accordingly

 Camera.Parameters params = camera.getParameters();
 params.setPreviewSize(your_width,your_height);
 camera.setParameters(params);
0

You cannot crop the camera preview image in a SurfaceView.

You can hide the upper half of a SurfaceView by simply overlaying it with some other view (normal XML layout feature). But be careful: SurfaceView does not behave exactly as a "real" view, and it does not always "play by the rules" of view hierarchy. Typically you can overcome these glitches by building the view hierarchy programmatically in onResume().

An alternative is to switch to TextureView, or use a SurfaceTexture directly and display it in an OpenGL context of your choice. In the latter case, you can partially overlay the camera preview texture with some other OpenGL object, e.g. display decoded video stream in the same OpenGL context.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I saw this, I have a YoutubeFragment at top side and when I set YoutubeFragment overlaying SurfaceView, my YoutubeFragment displayed like a part of SurfaceView. I also tried with TextureView but both Fragment cannot overlap, the top view was pushed down the bottom view – Cver Nov 28 '16 at 09:25
  • *YoutubeFragment displayed like a part of SurfaceView* - can you share a screenshot? This sounds very unusual – Alex Cohn Nov 28 '16 at 10:03
  • *both Fragment cannot overlap, the top view was pushed down the bottom view* - this may be simply that your XML layout is wrong – Alex Cohn Nov 28 '16 at 10:04
  • http://imgur.com/a/1pw9z youtube screen display as well as a camera preview – Cver Nov 28 '16 at 10:17
  • I don't think so, because i use a RelativeLayout as parent view and I don't set any attributes expect id,width,height for view of youtube and camera – Cver Nov 28 '16 at 10:24
  • What I see in your screenshot, is that the [YouTubePlayerView](https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerView) does not coexist with SurfaceView. This is not unexpected: both views are actually "holes" in the Android view layout system, optimized for hardware rendering. You could try ExoPlayer as an alternative (see e.g. http://stackoverflow.com/questions/14014087/views-overlayed-above-youtubeplayerfragment-or-youtubeplayerview-in-the-layout-h) – Alex Cohn Nov 28 '16 at 11:36
  • Wow, I just recently known ExoPlayer, actually my main application was complicated and if I change, there will be more trouble, I can ask is ExoPlayer can play youtube video and can run inside my fragment? Or should I edit my code if possible? – Cver Nov 28 '16 at 15:35
  • I am not an expert in ExoPlayer. You can read their documents. – Alex Cohn Nov 28 '16 at 20:45
0

For me adding enabling hardwareAccelerated in Manifest solved the problem of preview of CameraX coming in half portion although I set match_parent as height width of PreviewView

 <activity
        android:name=".ui.CameraActivity"
        android:label="@string/camera"
        android:hardwareAccelerated="true"/>
Astha Garg
  • 1,022
  • 7
  • 21