手机相机的使用步骤:
1.打开相机,一般会有前置和后置摄像头。
camera = Camera.open(cameId);
2.设置相机的参数,预览和图片尺寸。
//先获取相机参数
Camera.Parameters param=camera.getParameters();
获得相机支持的大小参数,然后通过参数去选择一个合适的尺寸。一般选择的是高度大于最小宽度,缩放比相近的参数。
private Camera.Size getPropPreviewSize(List<Camera.Size> list, float th, int minWidth){
Collections.sort(list, sizeComparator);
int i = 0;
for(Camera.Size s:list){
if((s.height >= minWidth) && equalRate(s, th)){
break;
}
i++;
}
if(i == list.size()){
i = 0;
}
return list.get(i);
}
将它们设置到参数中,一般设置预览和图片尺寸即可。
param.setPictureSize(picSize.width,picSize.height);
param.setPreviewSize(preSize.width,preSize.height);
camera.setParames(param);
将相机结果绘制到纹理,使用SurfaceTexture.
public void setPreviewTexture(SurfaceTexture texture){
if(camera!=null){
try {
camera.setPreviewTexture(texture);
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.预览
public boolean preview() {
if(camera!=null){
camera.startPreview();
}
return false;
}
加入权限
<uses-permission android:name="android.permission.CAMERA" />
检测是不是成功得到权限
public static void askPermission(Activity context, String[] permissions, int req, Runnable
runnable){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
int result= ActivityCompat.checkSelfPermission(context,permissions[0]);
if(result== PackageManager.PERMISSION_GRANTED){
runnable.run();
}else{
ActivityCompat.requestPermissions(context,new String[]{Manifest.permission.CAMERA
,Manifest.permission.WRITE_EXTERNAL_STORAGE},req);
}
}else{
runnable.run();
}
}
绘制
一般的我们通过纹理将相机的数据放入到纹理中,然后通过绘制纹理达到绘制相机的效果。我们的所有操作都是基于这个纹理进行的。
简单绘制绘制代码。
public class MyCameraDrawer implements GLSurfaceView.Renderer {
private SurfaceTexture surfaceTexture;
private float[] matrix=new float[16];
private int width,height
private int dataWidth,dataHeight;
private AFilter mOesFilter;
private int cameraId=1;
public MyCameraDrawer(Resources resources){
mOesFilter=new OesFilter(resources);
}
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
int texture = createTextureID();
surfaceTexture=new SurfaceTexture(texture);
mOesFilter.create();
mOesFilter.setTextureId(texture);
}
@Override
public void onSurfaceChanged(GL10 gl10, int i, int i1) {
setViewSize(width,height);
}
@Override
public void onDrawFrame(GL10 gl10) {
if(surfaceTexture!=null){
surfaceTexture.updateTexImage();
}
mOesFilter.draw();
}
public void setDataSize(int dataWidth,int dataHeight){
this.dataWidth=dataWidth;
this.dataHeight=dataHeight;
calculateMatrix();
}
public void setViewSize(int width,int height){
this.width=width;
this.height=height;
calculateMatrix();
}
private void calculateMatrix(){
Gl2Utils.getShowMatrix(matrix,this.dataWidth,this.dataHeight,this.width,this.height);
if(cameraId==1){
Gl2Utils.flip(matrix,true,false);
Gl2Utils.rotate(matrix,90);
}else{
Gl2Utils.rotate(matrix,270);
}
mOesFilter.setMatrix(matrix);
}
public SurfaceTexture getSurfaceTexture(){
return surfaceTexture;
}
public void setCameraId(int id){
this.cameraId=id;
calculateMatrix();
}
private int createTextureID(){
int[] texture = new int[1];
GLES20.glGenTextures(1, texture, 0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
return texture[0];
}
}
网友评论