
下面是我的条码扫描器活动,除了setautoFocusEnabled(true)之外,一切正常.它在运行时返回一条消息,说明我的设备不支持自动对焦,尽管三星Tab E T561是一个支持自动对焦的设备.
import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.util.Log;import androID.util.SparseArray;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import androID.vIEw.VIEw;import androID.Widget.EditText;import com.Google.androID.gms.vision.CameraSource;import com.Google.androID.gms.vision.Detector;import com.Google.androID.gms.vision.barcode.barcode;import com.Google.androID.gms.vision.barcode.barcodeDetector;import java.io.IOException;import static com.Google.androID.gms.vision.CameraSource.CAMERA_FACING_BACK;import static com.Google.androID.gms.vision.CameraSource.CAMERA_FACING_FRONT;public class ScanbarcodeActivity extends AppCompatActivity { private String TAG = "ScanbarcodeActivity"; private barcodeDetector barcodeDetector; private SurfaceVIEw cameraview; private CameraSource cameraSource; private EditText cardNo; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_scan_barcode); } @OverrIDe protected voID onResume() { cameraview = (SurfaceVIEw) findVIEwByID(R.ID.surfaceVIEwCamera); cardNo = (EditText) findVIEwByID(R.ID.editTextbarcode); scanbarcodeCam(0); super.onResume(); } @OverrIDe protected voID onDestroy() { if(cameraSource != null) { cameraSource.stop(); cameraSource.release(); cameraSource = null; } super.onDestroy(); } public voID switchCam(VIEw vIEw) { if(cameraSource.getCameraFacing() == CAMERA_FACING_BACK) { cameraSource.stop(); cameraSource.release(); cameraSource = null; scanbarcodeCam(0); Log.i(TAG, "switchCam to front"); } else { cameraSource.stop(); cameraSource.release(); cameraSource = null; scanbarcodeCam(1); Log.i(TAG, "switchCam to back"); } } public voID scanbarcodeCam(int cam) { if(barcodeDetector == null) { barcodeDetector = new barcodeDetector.Builder(this) .setbarcodeFormats(barcode.EAN_13) .build(); } if(cam == 0) { cameraSource = new CameraSource .Builder(this, barcodeDetector) .setRequestedPrevIEwSize(640, 480) .setFacing(CAMERA_FACING_FRONT) .setRequestedFps(30.0f) .build(); } else if(cam == 1) { cameraSource = new CameraSource .Builder(this, barcodeDetector) .setRequestedPrevIEwSize(640, 480) .setFacing(CAMERA_FACING_BACK) .setRequestedFps(30.0f) .setautoFocusEnabled(true) .build(); } if(!cameraview.getHolder().getSurface().isValID()) { Log.i(TAG, "*** new SurfaceHolder"); cameraview.getHolder().addCallback(new SurfaceHolder.Callback() { @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { try { cameraSource.start(cameraview.getHolder()); } catch (IOException | RuntimeException e) { Log.e(TAG, e.getMessage()); } } @OverrIDe public voID surfaceChanged(SurfaceHolder holder, int format, int wIDth, int height) { } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { if (cameraSource != null) { cameraSource.stop(); cameraSource.release(); cameraSource = null; } } }); } else { try { cameraSource.start(cameraview.getHolder()); } catch(IOException e) { Log.e(TAG, e.getMessage()); } } barcodeDetector.setProcessor(new Detector.Processor<barcode>() { @OverrIDe public voID release() { } @OverrIDe public voID receiveDetections(Detector.Detections<barcode> detections) { final SparseArray<barcode> barcodes = detections.getDetectedItems(); if(barcodes.size() != 0) { cardNo.post(new Runnable() { @OverrIDe public voID run() { cardNo.setText(barcodes.valueAt(0).displayValue); } }); } } }); }}任何帮助将受到高度赞赏.
解决方法:
所以经过两天的奋斗,我终于设法“炮制”了一个修复.但AndroID开发团队应该认真研究为什么setautoFocusEnabled(true)在使用auto Focus的设备上不起作用.
这是我的修复,希望它能节省一些时间:
@OverrIDepublic boolean ontouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_UP) { float x = event.getX(); float y = event.getY(); float touchMajor = event.gettouchMajor(); float touchMinor = event.gettouchMinor(); Rect touchRect = new Rect((int)(x - touchMajor / 2), (int)(y - touchMinor / 2), (int)(x + touchMajor / 2), (int)(y + touchMinor / 2)); this.submitFocusAreaRect(touchRect); } return super.ontouchEvent(event);}private voID submitFocusAreaRect(final Rect touchRect) { FIEld[] declaredFIElds = CameraSource.class.getDeclaredFIElds(); for (FIEld fIEld : declaredFIElds) { if (fIEld.getType() == Camera.class) { fIEld.setAccessible(true); try { Camera camera = (Camera) fIEld.get(cameraSource); if (camera != null) { Camera.Parameters cameraParameters = camera.getParameters(); if(cameraParameters.getMaxnumFocusAreas() == 0) { return; } Rect focusArea = new Rect(); focusArea.set(touchRect.left * 2000 / cameraview.getWIDth() - 1000, touchRect.top * 2000 / cameraview.getHeight() - 1000, touchRect.right * 2000 / cameraview.getWIDth() - 1000, touchRect.bottom * 2000 / cameraview.getHeight() - 1000); ArrayList<Camera.Area> focusAreas = new ArrayList<>(); focusAreas.add(new Camera.Area(focusArea, 1000)); cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_auto); cameraParameters.setFocusAreas(focusAreas); camera.setParameters(cameraParameters); camera.autoFocus(this); } } catch (illegalaccessexception | RuntimeException e) { e.getMessage(); } break; } }}现在我也可以使用后置摄像头扫描条形码.好极了!
总结以上是内存溢出为你收集整理的android – CameraSource .setAutoFocusEnabled(true)返回:虽然设备支持自动对焦,但此设备不支持相机自动对焦全部内容,希望文章能够帮你解决android – CameraSource .setAutoFocusEnabled(true)返回:虽然设备支持自动对焦,但此设备不支持相机自动对焦所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)