-2

A very standard overlay tutorial in this link was followed for developing the camera preview with overlay. However, when I change the <Button/> to <ImageView/> or <ImageButton/> the image doesn't get displayed. All I am getting is camera preview while using <ImageView/> and on using <ImageButton/> the images are not shown however little white lines at places where there should be images are displayed. The images are present in mipmap-xxhdpi. These buttons will be used to start another activity or take picture on click by user.

I've looked into questions on StackOverFlow but answer here doesn't work for me.

Other Relevant Details

The minSdkVersion is 16 and compileSdkVersion is 25. When I run the app in emulator on API-21 or above all I am getting is a black screen.

Q1: How to overlay <ImageView/> or <ImageButton/> on camera preview?

Q2: Would getting black screen have something to do with Camera API (old) being deprecated and I have to build separate classes to take into account different API?

Like the code below

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    //do something
} else {
    //do something else
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
chinmish
  • 99
  • 1
  • 11
  • Please do not vandalize your posts. Once you've posted a question, you have licensed the content to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](http://meta.stackoverflow.com/questions/323395/what-is-the-proper-route-for-a-dissociation-request). – Bugs Jun 30 '17 at 08:00
  • It's probably best to look at [ask] and take a look at [mcve] before making improvements. Stripping _everything_ out is not the way to go. – Bugs Jun 30 '17 at 11:43

2 Answers2

0

As per your first question I think the issue is that the elevation of the surface view used by you is more than that of an imageView and equal to the imageButton thus nothing shows in case of ImageView and lines show in case of ImageButton(hint: think 3d) here is a guide for android material design elevation and shadows.Try to increase the elevation of you ImageView or ImageButton.

Now for your second question, it should not be because of old API try this answer.

  • I tried your suggestions by using `bringToFront()` and `ViewCompat.setTranslationZ(view, translationZ)`. But the overlay layout didn't show up in the camera. I am still reading various sources on getting black screen on opening camera. The camera within the app works fine when opening it on API 16 to API 19 – chinmish Jun 23 '17 at 15:44
0

So my issue hasn't been solved in emulator. However, I was able to get hands on a range of devices (5) running API15,19,21,21,24. Here is the code that worked for me. API15 was used only after I changed minSdkVersion to 15.

Build your custom camera using the steps given here. When you will be editing your XML. Use this code

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"/>

<ImageView
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/your_file_name"/>
</FrameLayout>

As for getting black screen, the app had to be given permission within settings of the device(Settings->App->Select your app->Permisions->Camera) in API21 and above.

Edit: Got the same code working in emulator as well by invalidating cache and restarting followed by using clean project.

Community
  • 1
  • 1
chinmish
  • 99
  • 1
  • 11