本文介绍了带相机预览的TextureView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用TextureView在其中显示相机预览.最后,我想使用TextureView为相机预览设置不透明度.但是我有问题:
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:
这是我的班级代码:
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:
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" >
问题出在哪里?
推荐答案
您应该在manifest.xml中进行设置.
You should set this in you manifest.xml.
android:hardwareAccelerated="true"
在声明此内容的位置取决于您要在何处使用TextureView
,在服务中使用了它.因此,应在服务声明中声明它.
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"
/>
这篇关于带相机预览的TextureView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!