按下按钮后如何开始循环播放功能

时间:2019-04-02 17:45:39

标签: java android

我正在制作一个用作看守的android应用程序。现在,它的工作方式是这样的:按下按钮后,它会无意间拍摄图片,将其与上次拍摄的图片进行比较,如果有显着差异,它将保存该图片,发送短信并使用ftp上传图片。我是Java和Android的新手...

@Nullable
@Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        [...]
        //Take a picture
        view.findViewById(R.id.capture_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Take picture using the camera without preview.
                //...wanted to put here for loop but didn't work
                takePicture();
            }
        }
        return view;
    }

    // ----------------------------------------------------

   protected void takePicture() {
        if (mCameraPreview != null) {
            if (mCameraPreview.isSafeToTakePictureInternal()) {
                mCameraPreview.takePictureInternal();
            }
        } else {
            throw new RuntimeException("Background camera not initialized. Call startCamera() to initialize the camera.");
        }
    }

    // ----------------------------------------------------

@Override
    public void onImageCapture(@NonNull File imageFile) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap1 = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);

    // here it compares last and old picture 
    [...]

        // if comparison failed alert
        new FtpTask().execute(path);
    sendSMS("767555444", "ALARM ALARM");
    }

    // ----------------------------------------------------

void takePictureInternal() {
        safeToTakePicture = false;
        if (mCamera != null) {
            mCamera.takePicture(null, null, new Camera.PictureCallback() {
                @Override
                public void onPictureTaken(final byte[] bytes, Camera camera) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //Convert byte array to bitmap
                            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                            //Rotate the bitmap
                            Bitmap rotatedBitmap;
                            if (mCameraConfig.getImageRotation() != CameraRotation.ROTATION_0) {
                                rotatedBitmap = HiddenCameraUtils.rotateBitmap(bitmap, mCameraConfig.getImageRotation());

                                //noinspection UnusedAssignment
                                bitmap = null;
                            } else {
                                rotatedBitmap = bitmap;
                            }

                            //Save image to the file.
                            if (HiddenCameraUtils.saveImageFromFile(rotatedBitmap,
                                    mCameraConfig.getImageFile(),
                                    mCameraConfig.getImageFormat())) {
                                //Post image file to the main thread
                                new android.os.Handler(Looper.getMainLooper()).post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mCameraCallbacks.onImageCapture(mCameraConfig.getImageFile());
                                    }
                                });
                            } else {
                                //Post error to the main thread
                                new android.os.Handler(Looper.getMainLooper()).post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mCameraCallbacks.onCameraError(CameraError.ERROR_IMAGE_WRITE_FAILED);
                                    }
                                });
                            }

                            mCamera.startPreview();
                            safeToTakePicture = true;
                        }
                    }).start();
                }
            });
        } else {
            mCameraCallbacks.onCameraError(CameraError.ERROR_CAMERA_OPEN_FAILED);
            safeToTakePicture = true;
        }
    }

我想要实现的是,在按下按钮后,它启动了一个循环,在该循环中拍照并一次又一次地进行比较,直到我单击按钮停止为止。我不希望新的循环在旧的循环仍在处理之前开始(逐个图像比较需要时间)。更精确地说,我想问应该在哪里添加一个循环以使其起作用?

对不起,如果代码太多或太少。如果我忘记添加内容,请告诉我,然后我将上传。

1 个答案:

答案 0 :(得分:0)

听起来像通过摄像头捕获会话会更好。

请参阅本文-它应该可以帮助您了解您可以做什么:https://medium.com/androiddevelopers/understanding-android-camera-capture-sessions-and-requests-4e54d9150295