这样做的缺点是我的FPS非常低.有了这个低FPS,如果我不使用Asynctask在后台线程中执行此 *** 作,它在UI线程中做了太多工作.所以我尝试使用Asynctask进行相机 *** 作(我的主要目的是让UI仍然能够完美地工作,即使是相机预览回调中的繁重工作).
但即使在我使用Asynctask后,它也没有多大帮助.所以我想知道我的实现是错误的还是因为即使使用asynctask,UI线程仍会受到影响?
我的代码片段如下:
CameraActivity.java
public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("ACTIVITY_liFECYCLE","CameraActivity: onCreate"); setContentVIEw(R.layout.camera_layout);}@TargetAPI(Build.VERSION_CODES.HONEYCOMB)@OverrIDeprotected voID onResume() { Log.d("ACTIVITY_liFECYCLE","CameraActivity: onResume"); if(prevIEw == null){ prevIEw = new CameraPrevIEwAsync(this,camera); prevIEw.execute(); } super.onResume();}@OverrIDeprotected voID onPause() { Log.d("ACTIVITY_liFECYCLE","CameraActivity: onPause"); if(prevIEw!=null){ prevIEw.cancel(true); camera = prevIEw.getCamera(); if(camera!=null){ camera.stopPrevIEw(); camera.setPrevIEwCallback(null); camera.release(); camera = null; prevIEw.setCamera(camera); } prevIEw = null; } super.onPause();}@OverrIDepublic voID onDestroy(){ Log.d("ACTIVITY_liFECYCLE","CameraActivity: onDestroy"); super.onDestroy();}
CameraPrevIEwAsync.java:
private final String TAG = "CameraPrevIEwAsync";private CameraActivity camAct;private Camera mCamera;private int cameraID;private SurfaceVIEw mSurfaceVIEw;private SurfaceHolder mHolder;private boolean isPrevIEwRunning = false;private int[] rgbints;private int wIDth;private int height;private Bitmap mBitmap;public CameraPrevIEwAsync(CameraActivity act,Camera cam){ this.camAct = act; this.mCamera = cam; this.mSurfaceVIEw = (SurfaceVIEw) act.findVIEwByID(R.ID.surfaceVIEw);}public voID resetSurface(){ if(mCamera!=null){ mCamera.stopPrevIEw(); mCamera.setPrevIEwCallback(null); mCamera.release(); mCamera = null; } int tempID = R.ID.surfaceVIEw; relativeLayout buttonbar = (relativeLayout) camAct.findVIEwByID(R.ID.buttonbar); ((relativeLayout) camAct.findVIEwByID(R.ID.prevIEw)).removeAllVIEws(); SurfaceVIEw newSurface = new SurfaceVIEw(camAct); newSurface.setID(tempID); relativeLayout.LayoutParams layParams = new relativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); layParams.alignWithParent = true; newSurface.setLayoutParams(layParams); ((relativeLayout) camAct.findVIEwByID(R.ID.prevIEw)).addVIEw(newSurface); ((relativeLayout) camAct.findVIEwByID(R.ID.prevIEw)).addVIEw(buttonbar);}@OverrIDeprotected voID onPreExecute() { //Things to do before doInBackground executed Log.d(TAG,"onPreExecute"); relativeLayout.LayoutParams layParams = new relativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); layParams.alignWithParent = true; mSurfaceVIEw.setLayoutParams(layParams); //Check number of camera in the device,if less than 2 then remove swap button if (Camera.getNumberOfCameras() < 2) { ((relativeLayout) camAct.findVIEwByID(R.ID.buttonbar)).removeVIEwAt(R.ID.cameraSwap); } //opening the camera cameraID = findBackFacingCamera(); if (cameraID < 0) { cameraID = findFrontFacingCamera(); if (cameraID < 0) Toast.makeText(camAct,"No camera found.",Toast.LENGTH_LONG).show(); else mCamera = Camera.open(cameraID); } else { mCamera = Camera.open(cameraID); } //invalIDate the menu bar and show menu appropriately camAct.invalIDateOptionsMenu(); // get Camera parameters and set it to auto Focus if(mCamera!=null){ Camera.Parameters params = mCamera.getParameters(); List<String> focusModes = params.getSupportedFocusModes(); if (focusModes.contains(Camera.Parameters.FOCUS_MODE_auto)) { // set the focus mode params.setFocusMode(Camera.Parameters.FOCUS_MODE_auto); // set Camera parameters mCamera.setParameters(params); } } super.onPreExecute();}@OverrIDeprotected VoID doInBackground(VoID... params) { //Things to do in the background thread Log.d(TAG,"doInBackground"); mHolder = mSurfaceVIEw.getHolder(); mHolder.addCallback(surfaceCallback); return null;} @OverrIDeprotected voID onPostExecute(VoID values) { //Things to do after doInBackground Log.d(TAG,"onPostExecute");}@OverrIDeprotected voID onCancelled(){ super.onCancelled();}/* * ************************************************************************************ * SURFACEHolDER CALLBACK * ************************************************************************************ */SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() { @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { Log.d(TAG,"surfaceCreated!!"); if(CameraActivity.filterMode == CameraActivity.norMAL_FILTER){ try { if (mCamera != null) { mCamera.startPrevIEw(); mCamera.setPrevIEwdisplay(holder); }else{ Log.d(TAG,"CAMERA IS NulL in surfaceCreated!!"); } } catch (IOException exception) { Log.e(TAG,"IOException caused by setPrevIEwdisplay()",exception); } }else{ synchronized(mSurfaceVIEw){ if(isPrevIEwRunning){ return; }else{ mSurfaceVIEw.setwillNotDraw(false); if(mCamera!=null){ isPrevIEwRunning = true; Camera.Parameters p = mCamera.getParameters(); List<Size> sizes = p.getSupportedPrevIEwSizes(); Size size = p.getPrevIEwSize(); wIDth = size.wIDth; height = size.height; p.setPrevIEwFormat(ImageFormat.NV21); showSupportedCameraFormats(p); mCamera.setParameters(p); rgbints = new int[wIDth * height]; mCamera.startPrevIEw(); mCamera.setPrevIEwCallback(prevIEwCallback); } } } } } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { Log.d(TAG,"surfaceDestroyed!"); if(CameraActivity.filterMode == CameraActivity.norMAL_FILTER){ if (mCamera != null) { mCamera.stopPrevIEw(); isPrevIEwRunning = false; } }else{ synchronized(mSurfaceVIEw){ if(mCamera!=null){ mCamera.setPrevIEwCallback(null); mCamera.stopPrevIEw(); isPrevIEwRunning = false; } } } } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) { Log.d(TAG,"surfaceChanged!"); }};/* * ************************************************************************************ * CAMERA PREVIEW CALLBACK * ************************************************************************************ */Camera.PrevIEwCallback prevIEwCallback = new Camera.PrevIEwCallback() { @OverrIDe public voID onPrevIEwFrame(byte[] data,Camera camera) { if (!isPrevIEwRunning) return; Canvas resCanvas = null; if (mHolder == null) { return; } try { synchronized (mHolder) { resCanvas = mHolder.lockCanvas(null); int resCanvasW = resCanvas.getWIDth(); int resCanvasH = resCanvas.getHeight(); if(mBitmap == null){ mBitmap = Bitmap.createBitmap (wIDth,height,Bitmap.Config.ARGB_8888); } decodeYUV(rgbints,data,wIDth,height); Canvas canvas = new Canvas(mBitmap); //Setting the filter if(camAct.getCustomFilter().equalsIgnoreCase("norMAL")) ;//don't change the rgb value if(camAct.getCustomFilter().equalsIgnoreCase("GRAYSCALE")) rgbints = grayscale(rgbints); if(camAct.getCustomFilter().equalsIgnoreCase("INVERT")) rgbints = invert(rgbints); if(camAct.getCustomFilter().equalsIgnoreCase("BOOSTRED")) rgbints = boostcolor(rgbints,1); if(camAct.getCustomFilter().equalsIgnoreCase("BOOSTGREEN")) rgbints = boostcolor(rgbints,2); if(camAct.getCustomFilter().equalsIgnoreCase("BOOSTBLUE")) rgbints = boostcolor(rgbints,3); if(camAct.getCustomFilter().equalsIgnoreCase("NOISE")) rgbints = noise(rgbints); if(camAct.getCustomFilter().equalsIgnoreCase("HUE")) rgbints = hue(rgbints); if(camAct.getCustomFilter().equalsIgnoreCase("SATURATION")) rgbints = saturation(rgbints); if(camAct.getCustomFilter().equalsIgnoreCase("ENGRAVE")) rgbints = engrave(rgbints); if(camAct.getCustomFilter().equalsIgnoreCase("embosS")) rgbints = emboss(rgbints); // draw the decoded image,centered on canvas canvas.drawBitmap(rgbints,false,null); resCanvas.drawBitmap (mBitmap,resCanvasW-((wIDth+resCanvasW)>>1),resCanvasH-((height+resCanvasH)>>1),null); } } catch (Exception e){ e.printstacktrace(); } finally { // do this in a finally so that if an exception is thrown // during the above,we don't leave the Surface in an // inconsistent state if (resCanvas != null) { mHolder.unlockCanvasAndPost(resCanvas); } } }};
任何帮助深表感谢! :)先谢谢你们!
@H_502_30@解决方法 来自其他方法的回调被传递到调用open()的线程的事件循环.如果此线程没有事件循环,则回调将传递到主应用程序事件循环.如果没有主应用程序事件循环,则不会传递回调. Source 总结以上是内存溢出为你收集整理的Android:具有预览回调功能的Camera Asynctask全部内容,希望文章能够帮你解决Android:具有预览回调功能的Camera Asynctask所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)