Android – TextureView with camera preview

I want to use TextureView to display the camera preview. Finally, I want to use TextureView to set the opacity for the camera preview. But I have a problem:

This is my course 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 set hardware acceleration to true in the list:

<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" >

What's the problem?

resolvent:

Try 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" >

It works for me

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>