0

我只想在检测到人脸时保存图像,目前即使人脸不存在也正在保存。我只想在检测到人脸时捕获,我正在使用 android face API 进行人脸检测。

我的代码:在这里,我只想在 else 块中有面孔时才保存图像,我需要放置我的代码。根据我的代码,如果我在 else 块中开始捕获会话,我将无法访问 readerListener 那么如何执行此操作。

      final ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {

     @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    try {
                        image = reader.acquireLatestImage();
                        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                        byte[] bytes = new byte[buffer.capacity()];
                        buffer.get(bytes);
                        save(bytes);
                        mBitmapToSave1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        mBitmapToSave = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        Bitmap scaled = Bitmap.createScaledBitmap(mBitmapToSave, width, height, true);
                        int w = scaled.getWidth();
                        int h = scaled.getHeight();
                        // Setting post rotate to 90
                        Matrix mtx = new Matrix();
                        mtx.postRotate(-180);
                        // Rotating Bitmap
                        mBitmapToSave = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
                        // mBitmapToSave = Bitmap.createBitmap(width+rowPadding/pixelStride,height, Bitmap.Config.RGB_565);
                        // mBitmapToSave.copyPixelsToBuffer(buffer);

                        if (detector.isOperational() && mBitmapToSave != null) {
                            Frame frame = new Frame.Builder().setBitmap(mBitmapToSave).build();
                            SparseArray<Face> faces = detector.detect(frame);
                            for (index = 0; index < faces.size(); ++index) {
                                Face face = faces.valueAt(index);
                            }if (faces.size() == 0) {
                                MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.not);
                                mediaPlayer.start();
                                //Toast.makeText(AndroidCamera2API.this, "Face Not detected Adjust Camera Properly", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(AndroidCamera2API.this, "Face Found " + "\n", Toast.LENGTH_SHORT).show();
                            }

                            }
                        }catch(FileNotFoundException e){
                            e.printStackTrace();
                        } catch(IOException e){
                            e.printStackTrace();
                        } finally{
                            if (image != null) {
                                image.close();
                            }
                        }
                    }

                private void save(byte[] bytes) throws IOException {
                    OutputStream output = null;
                    try {
                        output = new FileOutputStream(file);
                        output.write(bytes);
                    } finally {
                        if (null != output) {
                            output.close();
                        }
                    }
                }
            };
            reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
            final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
                @Override
                public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                    super.onCaptureCompleted(session, request, result);
                    Toast.makeText(AndroidCamera2API.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
                    uploadMultipart();
                    createCameraPreview();

                }
 }
            };
            cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(CameraCaptureSession session) {
                    try {
                        session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onConfigureFailed(CameraCaptureSession session) {
                }
            }, mBackgroundHandler);
            mBitmapToSave = null;
        } catch(CameraAccessException e){
                e.printStackTrace();
            }
        }
4

0 回答 0