栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android AutoFocusCallback没有被调用或未返回

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android AutoFocusCallback没有被调用或未返回

您在预览类中的Oncreate上调用了自动对焦,因此应该在创建曲面或更改曲面时调用它,因为一旦开始camerapreview就会发生自动对焦,请查看此代码,我已经对此进行了工作并且正在工作

private void tryAutoFocus() {    if( MyDebug.LOG )        Log.d(TAG, "tryAutoFocus");    if( camera == null ) {        if( MyDebug.LOG ) Log.d(TAG, "no camera");    }    else if( !this.has_surface ) {        if( MyDebug.LOG ) Log.d(TAG, "preview surface not yet available");    }    else if( !this.is_preview_started ) {        if( MyDebug.LOG ) Log.d(TAG, "preview not yet started");    }    else if( is_taking_photo ) {        if( MyDebug.LOG ) Log.d(TAG, "currently taking a photo");    }    else {        // if a focus area is set, we always call autofocus even if it isn't supported, so we get the focus box        // otherwise, it's only worth doing it when autofocus has an effect (i.e., auto or macro mode)        Camera.Parameters parameters = camera.getParameters();        String focus_mode = parameters.getFocusMode();        if( has_focus_area || focus_mode.equals(Camera.Parameters.FOCUS_MODE_AUTO) || focus_mode.equals(Camera.Parameters.FOCUS_MODE_MACRO) ) { if( MyDebug.LOG )     Log.d(TAG, "try to start autofocus"); Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {     @Override     public void onAutoFocus(boolean success, Camera camera) {         if( MyDebug.LOG )  Log.d(TAG, "autofocus complete: " + success);         focus_success = success ? FOCUS_SUCCESS : FOCUS_FAILED;         focus_complete_time = System.currentTimeMillis();         if(_automationStarted){  takeSnapPhoto();         }     } }; this.focus_success = FOCUS_WAITING; this.focus_complete_time = -1; camera.autoFocus(autoFocusCallback);        }    }}public void surfaceCreated(SurfaceHolder holder) {    if( MyDebug.LOG )        Log.d(TAG, "surfaceCreated()");    // The Surface has been created, acquire the camera and tell it where to draw.    this.has_surface = true;    this.openCamera(false); // if setting up for the first time, we wait until the surfaceChanged call to start the preview    this.setWillNotDraw(false); // see http://stackoverflow.com/questions/2687015/extended-surfaceviews-ondraw-method-never-called}public void surfaceDestroyed(SurfaceHolder holder) {    if( MyDebug.LOG )        Log.d(TAG, "surfaceDestroyed()");    // Surface will be destroyed when we return, so stop the preview.    // Because the CameraDevice object is not a shared resource, it's very    // important to release it when the activity is paused.    this.has_surface = false;    this.closeCamera();}public void takeSnapPhoto() {    camera.setoneShotPreviewCallback(new Camera.PreviewCallback() {        @Override        public void onPreviewframe(byte[] data, Camera camera) { Camera.Parameters parameters = camera.getParameters(); int format = parameters.getPreviewFormat(); //YUV formats require more conversion if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {     int w = parameters.getPreviewSize().width;     int h = parameters.getPreviewSize().height;     // Get the YuV image     YuvImage yuv_image = new YuvImage(data, format, w, h, null);     // Convert YuV to Jpeg     Rect rect = new Rect(0, 0, w, h);     ByteArrayOutputStream output_stream = new ByteArrayOutputStream();     yuv_image.compressToJpeg(rect, 100, output_stream);     byte[] byt = output_stream.toByteArray();     FileOutputStream outStream = null;     try {         // Write to SD Card         File file = createFileInSDCard(FOLDER_PATH, "Image_"+System.currentTimeMillis()+".jpg");         //Uri uriSavedImage = Uri.fromFile(file);         outStream = new FileOutputStream(file);         outStream.write(byt);         outStream.close();     } catch (FileNotFoundException e) {         e.printStackTrace();     } catch (IOException e) {         e.printStackTrace();     } finally {     } }        }    });}

初始化相机时(或在这种情况下打开相机)

 private void openCamera(boolean start_preview) {      tryautofocus();//call this after setting camera parameters         }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574220.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号