开发者

Camera preview with a transparent image above

开发者 https://www.devze.com 2023-03-09 02:57 出处:网络
I would like to see an image on top of Camera SurfaceView\\preview. How do I do that开发者_StackOverflow社区? Any examples?A great example can be found in ZXing library, Barcode Scanner application.

I would like to see an image on top of Camera SurfaceView\preview. How do I do that开发者_StackOverflow社区? Any examples?


A great example can be found in ZXing library, Barcode Scanner application.

What they do is they use FrameLayout for SurfaceView and their custom ViewfinderView so that both SurfaceView and ViewfinderView are covering full screen:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">

  <SurfaceView android:id="@+id/preview_view"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_centerInParent="true"/>

  <com.google.zxing.client.android.ViewfinderView
      android:id="@+id/viewfinder_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@color/transparent"/>

</FrameLayout>

Note the fill_parent values in layout_height and layout_width for both views.

And then they draw custom things in ViewfinderView.onDraw() method, just on top of what is being displayed by camera preview.

If you don't want to draw anything, but just use predefined image, then use ImageView instead of ViewfinderView. You might consider calling setAlpha method to make your image transparent (if drawable wasn't already transparent by itself).


Here is a screenshot from Barcode Scanner:

Camera preview with a transparent image above

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号