【发布时间】:2015-10-22 10:25:24
【问题描述】:
我想使用 TextureView 在其中显示相机预览。最后,我想使用 TextureView 为相机预览设置不透明度。但我有问题:
10-22 12:21:14.773:W/TextureView(5126):TextureView 或子类只能在启用硬件加速的情况下使用。
这是我的课程代码:
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;
}
}
我在清单中将硬件加速设置为 true:
<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" >
问题出在哪里?
【问题讨论】:
-
您尝试了哪种设备?
-
主要问题是它适用于 Galaxy S4、LG G3。但它给了我关于 LG G2 的错误。它有安卓 4.4.2。所以它应该工作,但它没有
-
另外 - 使用相同方法的其他应用程序也可以工作。所以问题出在我的代码中
-
有迹象表明 G2(Tegra4 设备)上的硬件加速并未完全完善:Android Lollipop Hardware Acceleration Codec Error (H264). LG G2 / Galaxy Alpha。不过,我并不是出于个人经验。
-
但问题是这个解决方案适用于来自 Google Play 的一个应用程序。我使用了相同的解决方案,但它不起作用。
标签: android android-camera textureview