3

I would like to use TextureView to show camera preview in it. Finally i want to set opacity for camera preview, using TextureView. But i have problem:

10-22 12:21:14.773: W/TextureView(5126): A TextureView or a subclass can only be used with hardware acceleration enabled.

Here is my class code:

public class CameraService extends Service implements
    android.view.TextureView.SurfaceTextureListener {

private LayoutInflater layoutInflater;
private Camera mCamera;
private View mCameraView;
private WindowManager mWindowManager;
private TextureView textureView;
private float transparentLevel;

public CameraService() {
    transparentLevel = 0.5F;
}

public void onDestroy() {
    super.onDestroy();
    if (mWindowManager != null && mCameraView != null) {
        mWindowManager.removeView(mCameraView);
    }
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
}

public int onStartCommand(Intent intent, int i, int j) {
    android.view.WindowManager.LayoutParams layoutparams = new android.view.WindowManager.LayoutParams(
            100, 100, 2006, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, -2);
    mWindowManager = (WindowManager) getSystemService("window");
    layoutInflater = (LayoutInflater) getSystemService("layout_inflater");
    mCameraView = layoutInflater.inflate(R.layout.camera_surface, null);
    textureView = (TextureView) mCameraView.findViewById(R.id.textureView);
    textureView.setSurfaceTextureListener(this);
    textureView.setAlpha(transparentLevel);
    mWindowManager.addView(mCameraView, layoutparams);
    return 1;
}

public void onSurfaceTextureAvailable(SurfaceTexture surfacetexture, int i,
        int j) {
    mCamera = Camera.open();
    try {
        mCamera.setPreviewTexture(surfacetexture);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Parameters tmp = mCamera.getParameters();
    tmp.setPreviewSize(tmp.getSupportedPreviewSizes().get(0).width,tmp.getSupportedPreviewSizes().get(0).height);
    mCamera.setParameters(tmp);
    mCamera.startPreview();

}

public boolean onSurfaceTextureDestroyed(SurfaceTexture surfacetexture) {
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
    return false;
}

public void onSurfaceTextureSizeChanged(SurfaceTexture surfacetexture,
        int i, int j) {
    if (mCamera != null) {
        Parameters tmp = mCamera.getParameters();
        tmp.setPreviewSize(tmp.getSupportedPreviewSizes().get(0).width,tmp.getSupportedPreviewSizes().get(0).height);
        mCamera.setParameters(tmp);
        mCamera.startPreview();
    }
}

public void onSurfaceTextureUpdated(SurfaceTexture surfacetexture) {
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

I have hardware acceleration set to true in the manifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

Where is the problem?

fadden
  • 51,356
  • 5
  • 116
  • 166
  • Which device did you try? – Alex Cohn Oct 22 '15 at 11:12
  • The main problem is that it works on Galaxy S4, LG G3. But it give me error on LG G2. it has Android 4.4.2. So it should work, but it doesnt – Mikołaj Janeczek Oct 22 '15 at 19:47
  • Also - other apps using the same method works. so the problem is in my code – Mikołaj Janeczek Oct 22 '15 at 20:00
  • There is an indication that HW acceleration on G2 (a Tegra4 device) is not exactly polished: *[Android Lollipop Hardware Acceleration Codec Error (H264). LG G2 / Galaxy Alpha](http://stackoverflow.com/questions/30025778/android-lollipop-hardware-acceleration-codec-error-h264-lg-g2-galaxy-alpha)*. I am not speaking out of personal experience, though. – Alex Cohn Oct 22 '15 at 20:07
  • But the problem is that this solution works for one app from Google Play. I used the same solution and it didn't work. – Mikołaj Janeczek Oct 23 '15 at 10:08
  • Have you tried to set `android:hardwareAccelerated="true"` for the activity, as @ifeego proposed? You can also [check](http://stackoverflow.com/questions/14551656/android-textureview-and-hardware-acceleration) if the view is accelerated – Alex Cohn Oct 23 '15 at 12:33
  • Try to work without alpha and possibly other WindowManager flags, and if this resolves the hardwareAccelerated issue, restore the flags one by one. – Alex Cohn Oct 23 '15 at 12:46
  • Nope, changing WindowManager flasg didn't help me:( – Mikołaj Janeczek Oct 25 '15 at 10:39

2 Answers2

2

try using this...

<uses-feature
        android:name="android.hardware.camera"
        android:required="false"/>
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false"/>

<application
        android:name=".app.ExampleApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="true" >

This worked for me

Vennila
  • 240
  • 1
  • 11
2

You should set this in you manifest.xml.

android:hardwareAccelerated="true"

Where you declare this it's up to where you want to use your TextureView,you used it in your service.so you should declare this in your service declaration.

<service android:hardwareAccelerated="true"
 /> 
ifeegoo
  • 7,054
  • 3
  • 33
  • 38
  • Yes, there is an [indication](http://stackoverflow.com/questions/14551656/android-textureview-and-hardware-acceleration) that maybe marking ** – Alex Cohn Oct 22 '15 at 20:10