android Camera2 Hit超时进行jpeg回调

android Camera2 Hit超时进行jpeg回调,第1张

概述我正在使用新的相机API(camera2)在android5.0.2上创建一个应用程序.该应用程序每2.5秒拍摄一张照片,持续3个小时(总共4320张照片).正如您在下面的代码中看到的那样,我使用“timer”对重复的内容进行编码,而没有针对Capturepicturewithoutpreviewusingcamera2API的预览.我正

我正在使用新的相机API(camera2)在android 5.0.2上创建一个应用程序.该应用程序每2.5秒拍摄一张照片,持续3个小时(总共4320张照片).正如您在下面的代码中看到的那样,我使用“ timer”对重复的内容进行编码,而没有针对Capture picture without preview using camera2 API的预览.我正在使用NEXUS7 2013 16G 5.0.2进行测试.它对于开头的200-300张图片正常工作,并失败,并显示以下错误消息.失败总是以“ E / RequestThread-1:jpeg回调的命中超时!”开始,它必须触发某些事件.有人会帮助摆脱这种触发吗?否则,如果是AndroID BUG,这将在5.1.0中修复?

03-30 15:46:04.472  11432-11432/com.example.androID.camera2basic V/yo click﹕ ----   174 ---- click03-30 15:46:05.026  11432-11537/com.example.androID.camera2basic E/RequestThread-1﹕ Hit timeout for jpeg callback!03-30 15:46:05.027  11432-11537/com.example.androID.camera2basic W/CaptureCollector﹕ Jpeg buffers dropped for request: 17303-30 15:46:05.076  11432-11480/com.example.androID.camera2basic E/CameraDevice-JV-1﹕ Lost output buffer reported for frame 17303-30 15:46:05.090  11432-11537/com.example.androID.camera2basic W/LegacyRequestMapper﹕ convertRequestMetadata - control.awbRegions setting is not supported, ignoring value03-30 15:46:05.090  11432-11537/com.example.androID.camera2basic W/LegacyRequestMapper﹕ Only received metering rectangles with weight 0.03-30 15:46:05.091  11432-11537/com.example.androID.camera2basic W/LegacyMetadataMapper﹕ convertAfModetoLegacy - ignoring unsupported mode 4, defaulting to fixed03-30 15:46:05.091  11432-11537/com.example.androID.camera2basic W/LegacyRequestMapper﹕ convertRequestToMetadata - Ignoring androID.lens.focusdistance false, only 0.0f is supported03-30 15:46:05.098  11432-11537/com.example.androID.camera2basic E/AndroIDRuntime﹕ FATAL EXCEPTION: RequestThread-1Process: com.example.androID.camera2basic, PID: 11432java.lang.RuntimeException: startPrevIEw Failed        at androID.harDWare.Camera.startPrevIEw(Native Method)        at androID.harDWare.camera2.legacy.RequestThreadManager.startPrevIEw(RequestThreadManager.java:275)        at androID.harDWare.camera2.legacy.RequestThreadManager.doJpegCapturePrepare(RequestThreadManager.java:288)        at androID.harDWare.camera2.legacy.RequestThreadManager.access00(RequestThreadManager.java:61)        at androID.harDWare.camera2.legacy.RequestThreadManager.handleMessage(RequestThreadManager.java:767)        at androID.os.Handler.dispatchMessage(Handler.java:98)        at androID.os.Looper.loop(Looper.java:135)        at androID.os.HandlerThread.run(HandlerThread.java:61)

这是我的代码:

public class CameraActivity extends Activity {    Timer mTimer = null;    Handler mHandler = new Handler();    private ImageReader imageReader;    private Handler backgroundHandler;    private HandlerThread backgroundThread;    private String cameraID;    private CameraDevice cameraDevice;    private CameraCaptureSession cameraCaptureSession;    static int count = 0;    static int count2 = 0;    /**     * Conversion from screen rotation to JPEG orIEntation.     */    private static final SparseIntArray ORIENTATIONS = new SparseIntArray();    static {        ORIENTATIONS.append(Surface.ROTATION_0, 90);        ORIENTATIONS.append(Surface.ROTATION_90, 0);        ORIENTATIONS.append(Surface.ROTATION_180, 270);        ORIENTATIONS.append(Surface.ROTATION_270, 180);    }@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //setContentVIEw(R.layout.activity_camera);    setContentVIEw(R.layout.activity_main);    button takePicture = (button)findVIEwByID(R.ID.takepic);    takePicture.setonClickListener(onClickPicture);    //(1) setting up camera but stop before camera createCaptureRequest    setupCamera2();}private voID setupCamera2() {    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);    try {        for (String cameraID : manager.getCameraIDList()) {        Cameracharacteristics characteristics = manager.getCameracharacteristics(cameraID);        //if (characteristics.get(Cameracharacteristics.LENS_FACING) != Cameracharacteristics.LENS_FACING_BACK) {        if (characteristics.get(Cameracharacteristics.LENS_FACING) != Cameracharacteristics.LENS_FACING_FRONT) {            continue;        }    StreamConfigurationMap configs = characteristics.get(         Cameracharacteristics.SCALER_STREAM_CONfigURATION_MAP);                  this.cameraID = cameraID;    manager.openCamera(this.cameraID, cameraStateCallback, backgroundHandler);            Size[] sizes = configs.getoutputSizes(ImageFormat.JPEG);        int   picWIDth = 640;//1920;            int   picHeight = 480;//1080;        imageReader = ImageReader.newInstance(picWIDth, picHeight, ImageFormat.JPEG, 2);        imageReader.setonImageAvailableListener(onImageAvailableListener, backgroundHandler);        }    } catch (CameraAccessException | NullPointerException e) {        e.printstacktrace();    }}private final CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() {    @OverrIDe    public voID onopened(CameraDevice device) {        cameraDevice = device;    //(2) Camera capture session        createCameraCaptureSession();    }    @OverrIDe    public voID ondisconnected(CameraDevice cameraDevice) {}    @OverrIDe    public voID one rror(CameraDevice cameraDevice, int error) {}};    //private voID createCaptureSession() {private voID createCameraCaptureSession() {    List<Surface> outputSurfaces = new linkedList<>();    outputSurfaces.add(imageReader.getSurface());    Log.v("-yo(2)-", "in createcameraCaptureSession Now");    try {        cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {        @OverrIDe        public voID onConfigured(CameraCaptureSession session) {        //cameraCaptureSession = session;        cameraCaptureSession = session;        //commented out to invoked from button         //createCaptureRequest();        }           @OverrIDe       public voID onConfigureFailed(CameraCaptureSession session) {}        }, null);    } catch (CameraAccessException e) {        e.printstacktrace();    }}private final ImageReader.OnImageAvailableListener onImageAvailableListener = new ImageReader.OnImageAvailableListener() {    @OverrIDe    public voID onImageAvailable(ImageReader reader) {        //createCaptureRequest();    Log.v("yo ireader ","----   "+(count2++)+" ---- ireader");      //Image mImage = imageReader.acquireLatestimage();        Image mImage = reader.acquireLatestimage();        file mfile = new file(Environment.getExternalStorageDirectory() + "/yP2PTEST/0P2Pimage.jpg");    Log.v("--yo--", "In ImageReader Now writing to "+mfile);    /////////////////////////////////////            ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();            byte[] bytes = new byte[buffer.remaining()];            buffer.get(bytes);            fileOutputStream output = null;            try {                output = new fileOutputStream(mfile);                output.write(bytes);            } catch (fileNotFoundException e) {                e.printstacktrace();            } catch (IOException e) {                e.printstacktrace();            } finally {                mImage.close();                if (null != output) {                    try {                        output.close();                    } catch (IOException e) {                        e.printstacktrace();                    }                }            }        ImageVIEw curPic = (ImageVIEw)findVIEwByID(R.ID.imageVIEw1);        Bitmap mCurrentBitmap = BitmapFactory.decodefile(mfile.getPath());        curPic.setimageBitmap(mCurrentBitmap);         }    ///////////////////////////////////    };private voID createCaptureRequest() {    Log.v("-yo(3)-", "in createCaptureRequest Now");    try {        CaptureRequest.Builder requestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);        requestBuilder.addTarget(imageReader.getSurface());        // Focus        requestBuilder.set(CaptureRequest.CONTRol_AF_MODE, CaptureRequest.CONTRol_AF_MODE_CONTINUOUS_PICTURE);        // OrIEntation        //yo int rotation = windowManager.getDefaultdisplay().getRotation();        int rotation = this.getwindowManager().getDefaultdisplay().getRotation();        requestBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));       // cameraCaptureSession.capture(requestBuilder.build(), camera2Callback, null);        cameraCaptureSession.capture(requestBuilder.build(), mCaptureCallback, null);    } catch (CameraAccessException e) {        e.printstacktrace();    }}    CameraCaptureSession.CaptureCallback mCaptureCallback            = new CameraCaptureSession.CaptureCallback() {        @OverrIDe        public voID onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,                                       TotalCaptureResult result) {            //showToast("JPEG Saved : ");            //Log.v("yo save","- saved JPEG -");            //unlockFocus();        }    };    private Handler mMessageHandler = new Handler() {        @OverrIDe        public voID handleMessage(Message msg) {            if (this != null) {                Toast.makeText(CameraActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();            }        }    };    private voID showToast(String text) {        // We show a Toast by sending request message to mMessageHandler. This makes sure that the        // Toast is shown on the UI thread.        Message message = Message.obtain();        message.obj = text;        mMessageHandler.sendMessage(message);    }//------------------------------------------------------------//    public VIEw.OnClickListener onClickPicture = new VIEw.OnClickListener() {        public voID onClick(VIEw v) {        /*-------  camera2   --------------*/        mTimer = null;        mTimer = new Timer(true);        mTimer.schedule( new TimerTask(){            @OverrIDe            public voID run() {                /*------------------------*/                mHandler.post( new Runnable() {                public voID run() {                    createCaptureRequest();                     Log.v("yo click ","----   "+(count++)+" ---- click");                }                });            }            }, 1000, 2500);//1500,1600, 1800 etc        };    };};

提前致谢.

编辑
我调查了CAMERA2 API的源程序,并发现了错误消息的出处.
JPEG_FRAME_TIMEOUT当前为300ms,我猜它太小了,想要增加它.如果有人知道该怎么做,请告诉我?

if (holder.hasJpegTargets()) {                            doJpegCapture(holder);                            if (!mReceivedJpeg.block(JPEG_FRAME_TIMEOUT)) {                                Log.e(TAG, "Hit timeout for jpeg callback!");                                mCaptureCollector.failNextJpeg();                            }                        }

解决方法:

修改过的Google示例遇到了相同的问题.
拍完两张照片后,出现了此错误,因此您只需要根据需要扩展最后一个参数,它就可以工作.

    imageReader = ImageReader.newInstance(largest.getWIDth(), largest.getHeight(),ImageFormat.JPEG, 2);

但是请记住以下几点:
@param maxImages用户想要同时访问的最大图像数.该值应尽可能小以限制内存使用.用户获得maxImages图像后,必须释放其中一个图像,然后才能通过{@link #acquireLatestimage()}或{@link #acquireNextimage()}访问新图像.必须大于0.

总结

以上是内存溢出为你收集整理的android Camera2 Hit超时进行jpeg回调全部内容,希望文章能够帮你解决android Camera2 Hit超时进行jpeg回调所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078736.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存