android 7自定义相机预览及拍照功能
本文实例为大家分享了Android实现摄像头切换,拍照及保存到相册,预览等功能,解决android7拍照之后不能连续预览的问题、参数设置相关问题以及前后摄像头语言颠倒等问题。
importandroid.Manifest; importandroid.app.Activity; importandroid.content.Context; importandroid.content.Intent; importandroid.content.pm.PackageManager; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.graphics.Matrix; importandroid.graphics.PixelFormat; importandroid.hardware.Camera; importandroid.net.Uri; importandroid.os.Build; importandroid.os.Bundle; importandroid.os.Environment; importandroid.os.Handler; importandroid.support.v4.app.ActivityCompat; importandroid.util.Log; importandroid.view.KeyEvent; importandroid.view.SurfaceHolder; importandroid.view.SurfaceView; importandroid.view.View; importandroid.view.WindowManager; importandroid.widget.TextView; importcom.hama.tracker.R; importjava.io.File; importjava.io.FileOutputStream; importjava.io.IOException; importjava.util.ArrayList; importjava.util.Iterator; importjava.util.List; publicclassCamaraActivityextendsActivityimplementsView.OnClickListener{ @Bind(R.id.return_back) TextViewreturnBack; @Bind(R.id.tvswitch_camera) TextViewtvswitchCamera; //启动摄像机 privateCameramCamera; publicbooleanisopen_camara=false; privateSurfaceViewsurfaceView; privateSurfaceHoldermholder=null; privateSurfaceCallbackpreviewCallBack; privatebooleanisTakingPhoto;//是否正在拍照 @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); verifyPermission(newString[]{Manifest.permission.CAMERA}); //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_camara); ButterKnife.bind(this); //预览控件 surfaceView=(SurfaceView)this .findViewById(R.id.surfaceView); //设置参数 surfaceView.getHolder().setKeepScreenOn(true); surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); returnBack.setOnClickListener(this); tvswitchCamera.setOnClickListener(this); EventBus.getDefault().register(this); } publicvoidverifyPermission(String[]permissions){ if(permissions!=null){ Listlists=newArrayList<>(); for(inti=0;i 0){ String[]ps=newString[lists.size()]; for(inti=0;i sizeList=parameters.getSupportedPreviewSizes(); //如果sizeList只有一个我们也没有必要做什么了,因为就他一个别无选择 if(sizeList.size()>1){ Iterator itor=sizeList.iterator(); while(itor.hasNext()){ Camera.Sizecur=itor.next(); /*if(cur.width>=PreviewWidth &&cur.height>=PreviewHeight){*/ if(cur.width>=PreviewWidth &&cur.height>=PreviewHeight){ PreviewWidth=cur.width; PreviewHeight=cur.height; break; } } } parameters.setPreviewSize(PreviewWidth,PreviewHeight);//获得摄像区域的大小 //parameters.setPreviewFrameRate(3);//每秒3帧每秒从摄像头里面获得3个画面 //parameters.setPreviewFpsRange(3,); List list=parameters.getSupportedPreviewFpsRange(); int[]v=null; intindex=0; intmin=0; for(inti=0;i min){ min=v[0]; index=i; } } parameters.setPreviewFpsRange(list.get(index)[0],list.get(index)[1]); parameters.setPictureFormat(PixelFormat.JPEG);//设置照片输出的格式 parameters.set("jpeg-quality",85);//设置照片质量 parameters.setPictureSize(PreviewWidth,PreviewHeight);//设置拍出来的屏幕大小 parameters.setRotation(180);//Java部分 if(Build.VERSION.SDK_INT>=24){ if(postion==0){ mCamera.setDisplayOrientation(90); }else{ mCamera.setDisplayOrientation(270); } }else{ mCamera.setDisplayOrientation(90); } mCamera.setParameters(parameters);//把上面的设置赋给摄像头 mCamera.setPreviewDisplay(mySurfaceView);//把摄像头获得画面显示在SurfaceView控件里面 mholder=mySurfaceView; mCamera.setPreviewCallback(newCamera.PreviewCallback(){ @Override publicvoidonPreviewFrame(byte[]data,Cameracamera){ } }); mCamera.startPreview();//开始预览 //mPreviewRunning=true; }catch(IOExceptione){ Log.e("tag",e.toString()); } } Handlerhandler=null; privatevoidautoTakePhoto(){ //拍照前需要对焦获取清析的图片 if(null==mCamera)return; mCamera.autoFocus(newCamera.AutoFocusCallback(){ @Override publicvoidonAutoFocus(booleansuccess,Cameracamera){ if(success&&isopen_camara){ //对焦成功 //Toast.makeText(MainActivity.this,"对焦成功!!",Toast.LENGTH_SHORT).show(); if(!isTakingPhoto){ isTakingPhoto=true; handler=newHandler(); handler.post(newRunnable(){ @Override publicvoidrun(){ mCamera.takePicture(null,null,newMyPictureCallback()); } }); } } } }); } //照片回调 privatefinalclassMyPictureCallbackimplementsCamera.PictureCallback{ //照片生成后 @Override publicvoidonPictureTaken(byte[]data,Cameracamera){ try{ Bitmapbitmap=BitmapFactory.decodeByteArray(data,0,data.length); Matrixmatrix=newMatrix(); matrix.setRotate(270); FilejpgFile=newFile(Environment.getExternalStorageDirectory()+"/DCIM/camera"); if(!jpgFile.exists()){ jpgFile.mkdir(); } FilejpgFile1=newFile(jpgFile.getAbsoluteFile(),System.currentTimeMillis()+".jpg"); bitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); FileOutputStreamfos=newFileOutputStream(jpgFile1); bitmap.compress(Bitmap.CompressFormat.JPEG,80,fos); ToastUtils.show(getApplicationContext(),getString(R.string.save_success)); fos.close(); Intentintent=newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uriuri=Uri.fromFile(jpgFile1); intent.setData(uri); sendBroadcast(intent); }catch(Exceptione){ e.printStackTrace(); }finally{ if(Build.VERSION.SDK_INT>=24){ reset(); } isTakingPhoto=false; } } } privateintcameraPosition=1;//0代表前置摄像头,1代表后置摄像头 privatevoidswitchCamara(){ //切换前后摄像头 intcameraCount=0; Camera.CameraInfocameraInfo=newCamera.CameraInfo(); cameraCount=Camera.getNumberOfCameras();//得到摄像头的个数 for(inti=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。