我想使用Camera2 API从相机中获取单个帧并使用ImageVIEw显示它.
我发现了一些密切的问题,例如
https://stackoverflow.com/questions/25462277/camera-preview-image-data-processing-with-android-l-and-camera2-api
而且我也看过Camera2Basic示例,但是它太复杂了,并不是我真正需要的.
我写的代码是基于我在网络上看到的一些示例,应该可以执行,但无法正常工作,我不知道为什么.
该应用程序不会崩溃,但不会在ImageVIEw上显示任何内容.
我在任何函数调用中都使用了Log消息,以尝试使logcat保持清晰.
另外,该应用程序的logcat表示“该应用程序可能在后台执行了太多工作.”我看不到这是怎么回事,因为我发出了单个captureRequest而不是RepeatingCaptureRequest.
这是代码和logcat:
码:
public class CameraimageReaderActivity extends AppCompatActivity {private final static String TAG = "CAMERA_IMAGE_READY: ";private ImageReader imageReader;private String cameraID;private CameraDevice camera;private HandlerThread handlerThread;private Handler handler;private Surface imageReaderSurface;private ImageVIEw imageVIEw;private CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() { @OverrIDe public voID onopened(CameraDevice cameraDevice) { Log.d(TAG, "onopend: CAMERA OPENED"); camera = cameraDevice; getFrames(); } @OverrIDe public voID ondisconnected(CameraDevice cameraDevice) { Log.d(TAG, "ondisconnected: CAMERA disCONNECTED"); cameraDevice.close(); camera = null; } @OverrIDe public voID one rror(CameraDevice cameraDevice, int i) { Log.d(TAG, "onError: CAMERA ERROR"); cameraDevice.close(); camera = null; }};private CameraCaptureSession.StateCallback captureSessionStateCallback = new CameraCaptureSession.StateCallback() { @OverrIDe public voID onConfigured(CameraCaptureSession cameraCaptureSession) { Log.d(TAG, "onConfigured: build request and capture"); try { CaptureRequest.Builder requestBuilder = cameraCaptureSession.getDevice().createCaptureRequest(CameraDevice.TEMPLATE_RECORD); requestBuilder.addTarget(imageReaderSurface); cameraCaptureSession.capture(requestBuilder.build(), null, handler); } catch (CameraAccessException e) { Log.d(TAG, "onConfigured: CANT CREATE CAPTURE REQUEST"); e.printstacktrace(); } } @OverrIDe public voID onConfigureFailed(CameraCaptureSession cameraCaptureSession) { Log.d(TAG, "onConfiguredFailed: CANT CONfigURE CAMERA"); }};private ImageReader.OnImageAvailableListener imageReaderListener = new ImageReader.OnImageAvailableListener() { @OverrIDe public voID onImageAvailable(ImageReader imageReader) { Log.d(TAG, "onImageAvailable: IMAGE AVAILABLE"); Image image = imageReader.acquireLatestimage(); int imgFormat = image.getFormat(); ByteBuffer pixelArray1 = image.getPlanes()[0].getBuffer(); int pixelStrIDe = image.getPlanes()[0].getPixelStrIDe(); int rowStrIDe = image.getPlanes()[0].getRowStrIDe(); int rowpadding = rowStrIDe - pixelStrIDe * 640; Bitmap bitmap = Bitmap.createBitmap(640 + rowpadding/pixelStrIDe, 480, Bitmap.Config.RGB_565); bitmap.copyPixelsFromBuffer(pixelArray1); imageVIEw.setimageBitmap(bitmap); image.close(); }};/** * Sets the cameraID with the front camera ID and sets imageReader propertIEs. */public voID setupCamera(int wIDth, int height) { imageReader = ImageReader.newInstance(wIDth, height, ImageFormat.RGB_565, 30); CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE); try { for (String allCamerasID : cameraManager.getCameraIDList()) { Cameracharacteristics cameracharacteristics = cameraManager.getCameracharacteristics(allCamerasID); if (cameracharacteristics.get(Cameracharacteristics.LENS_FACING) == Cameracharacteristics.LENS_FACING_FRONT) { continue; } cameraID = allCamerasID; Log.d(TAG, "setupCamera: CameraID is: " + cameraID); return; } } catch (CameraAccessException e) { e.printstacktrace(); }}/** * Connects to the front facing camera. * After the connection to the camera, the onopened callback method will be invoked. */public voID connectCamera() { CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE); try { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { Log.d(TAG, "CANT OPEN CAMERA"); // Todo: ConsIDer calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overrIDing // public voID onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } cameraManager.openCamera(cameraID, cameraStateCallback, handler); Log.d(TAG, "connectCamera: CAMERA OPENED!"); } catch (CameraAccessException e) { e.printstacktrace(); }}/** * Build the captureSessionRequest and start in repeat. */public voID getFrames() { Log.d(TAG, "getFrames: CREATE CAPTURE SESSION"); imageReaderSurface = imageReader.getSurface(); List<Surface> surfaceList = new ArrayList<>(); surfaceList.add(imageReaderSurface); try { camera.createCaptureSession(surfaceList, captureSessionStateCallback, handler); } catch (CameraAccessException e) { e.printstacktrace(); }}public voID startBackgroundThread() { handlerThread = new HandlerThread("CameraimageReaderActivity"); handlerThread.start(); handler = new Handler(handlerThread.getLooper());}public voID stopBackgroundThread() { handlerThread.quitSafely(); try { handlerThread.join(); handlerThread = null; handler = null; } catch (InterruptedException e) { e.printstacktrace(); }}public voID closeCamera() { if (camera != null) { camera.close(); camera = null; }}@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_camera_image_reader); imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imageVIEw); setupCamera(640, 480); connectCamera();}@OverrIDeprotected voID onPause() { closeCamera(); startBackgroundThread(); super.onPause();}@OverrIDeprotected voID onResume() { super.onResume(); startBackgroundThread(); //connectCamera();}
和(相关的)logcat:
03-22 14:27:32.900 18806-18806/com.example.noamm_000.talkwithcompviawifi D/CAMERA_IMAGE_READY:: setupCamera: CameraID is: 003-22 14:27:32.904 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value mw_continuous-picture03-22 14:27:32.905 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value emboss03-22 14:27:32.905 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value sketch03-22 14:27:32.905 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value neon03-22 14:27:32.905 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value asd03-22 14:27:32.905 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value backlight03-22 14:27:32.905 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value flowers03-22 14:27:32.905 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value AR03-22 14:27:32.912 18806-18806/com.example.noamm_000.talkwithcompviawifi I/CameraManager: Using legacy camera HAL.03-22 14:27:33.685 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value mw_continuous-picture03-22 14:27:33.685 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value emboss03-22 14:27:33.685 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value sketch03-22 14:27:33.685 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value neon03-22 14:27:33.686 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value asd03-22 14:27:33.686 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value backlight03-22 14:27:33.686 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value flowers03-22 14:27:33.686 18806-18806/com.example.noamm_000.talkwithcompviawifi W/ArrayUtils: Ignoring invalID value AR03-22 14:27:33.702 18806-18806/com.example.noamm_000.talkwithcompviawifi D/CAMERA_IMAGE_READY:: connectCamera: CAMERA OPENED!03-22 14:27:33.719 18806-18806/com.example.noamm_000.talkwithcompviawifi I/Choreographer: Skipped 56 frames! The application may be doing too much work on its main thread.03-22 14:27:33.787 18806-18806/com.example.noamm_000.talkwithcompviawifi D/CAMERA_IMAGE_READY:: onopend: CAMERA OPENED03-22 14:27:33.787 18806-18806/com.example.noamm_000.talkwithcompviawifi D/CAMERA_IMAGE_READY:: getFrames: CREATE CAPTURE SESSION03-22 14:27:33.789 18806-18806/com.example.noamm_000.talkwithcompviawifi I/CameraDeviceState: Legacy camera service Transitioning to state CONfigURING03-22 14:27:33.789 18806-19149/com.example.noamm_000.talkwithcompviawifi I/RequestThread-0: Configure outputs: 1 surfaces configured.03-22 14:27:33.790 18806-19149/com.example.noamm_000.talkwithcompviawifi D/Camera: app passed NulL surface03-22 14:27:33.838 18806-18806/com.example.noamm_000.talkwithcompviawifi I/CameraDeviceState: Legacy camera service Transitioning to state IDLE03-22 14:27:33.843 18806-19150/com.example.noamm_000.talkwithcompviawifi D/CAMERA_IMAGE_READY:: onConfigured: build request and capture03-22 14:27:33.874 18806-19149/com.example.noamm_000.talkwithcompviawifi W/LegacyRequestMapper: convertRequestMetadata - control.awbRegions setting is not supported, ignoring value03-22 14:27:33.875 18806-19149/com.example.noamm_000.talkwithcompviawifi W/LegacyRequestMapper: Only received metering rectangles with weight 0.03-22 14:27:33.875 18806-19149/com.example.noamm_000.talkwithcompviawifi W/LegacyRequestMapper: Only received metering rectangles with weight 0.03-22 14:27:34.070 18806-18806/com.example.noamm_000.talkwithcompviawifi I/Timeline: Timeline: Activity_IDle ID: androID.os.BinderProxy@13c07070 time:33114368303-22 14:27:34.317 18806-19155/com.example.noamm_000.talkwithcompviawifi I/CameraDeviceState: Legacy camera service Transitioning to state CAPTURING03-22 14:27:34.353 18806-19149/com.example.noamm_000.talkwithcompviawifi I/CameraDeviceState: Legacy camera service Transitioning to state IDLE03-22 14:27:34.403 18806-18806/com.example.noamm_000.talkwithcompviawifi D/BubblePopupHelper: isShowingBubblePopup : false03-22 14:27:34.403 18806-18806/com.example.noamm_000.talkwithcompviawifi D/BubblePopupHelper: isShowingBubblePopup : false03-22 14:27:34.404 18806-18806/com.example.noamm_000.talkwithcompviawifi D/BubblePopupHelper: isShowingBubblePopup : false03-22 14:27:34.404 18806-18806/com.example.noamm_000.talkwithcompviawifi D/BubblePopupHelper: isShowingBubblePopup : false03-22 14:28:07.684 18806-18823/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned03-22 14:28:07.684 18806-18822/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned03-22 14:28:07.684 18806-19184/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned03-22 14:28:07.685 18806-18823/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned03-22 14:28:07.685 18806-18822/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned03-22 14:28:07.685 18806-19184/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned03-22 14:28:07.686 18806-18823/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned03-22 14:28:07.686 18806-18822/com.example.noamm_000.talkwithcompviawifi E/BufferQueueProducer: [unnamed-18806-1] cancelBuffer: BufferQueue has been abandoned
谢谢,
诺姆
解决方法:
尝试这种方式更好地在后台线程中处理它.
public voID onImageAvailable(ImageReader reader) { new ImageSaver(reader.acquireLatestimage()); } private class ImageSaver implements Runnable { private final Image mImage; public ImageSaver(Image image) { mImage = image; } @OverrIDe public voID run() { file mImagefilename = null; if (mImage != null) { ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer(); byte[] bytes = new byte[byteBuffer.remaining()]; byteBuffer.get(bytes); fileOutputStream fileOutputStream = null; try { mImagefilename = createImagefilename(); fileOutputStream = new fileOutputStream(mImagefilename); fileOutputStream.write(bytes); } catch (IOException e) { e.printstacktrace(); } finally { mImage.close(); if (mImagefilename != null) { Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_file); mediaStoreUpdateIntent.setData(Uri.fromfile(mImagefilename)); sendbroadcast(mediaStoreUpdateIntent); loadImageFromStorage(mImagefilename); } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printstacktrace(); } } } } } private voID loadImageFromStorage(file mImagefilename) { imageVIEw.setimageBitmap(BitmapFactory.decodefile(mImagefilename.getabsolutePath())); } } private file createImagefilename() throws IOException { String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String prepend = "IMAGE_" + timestamp + "_"; file imagefile = file.createTempfile(prepend, ".jpg", createImageFolder()); return imagefile; } private file createImageFolder() { file imagefile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); file mImageFolder = new file(imagefile, "myFolder"); if (!mImageFolder.exists()) { mImageFolder.mkdirs(); } return mImageFolder; }
总结 以上是内存溢出为你收集整理的android-使用camera2 API获取单个图像并使用ImageView显示全部内容,希望文章能够帮你解决android-使用camera2 API获取单个图像并使用ImageView显示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)