簡體   English   中英

如何在按下按鈕后開始循環函數

[英]How to start looping a function after button was pressed

我正在制作一個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;
        }
    }

我想要實現的是按下按鈕后開始循環拍攝照片並一次又一次地比較,直到我點擊按鈕停止。 我不希望在舊的循環仍處理之前啟動新循環(逐個像素的圖像比較需要時間)。 更精確的是,我想問一下我應該在哪里添加循環以使其有效?

很抱歉,如果代碼太多或太少。 如果我忘記添加內容請告訴我,我會上傳。

聽起來你會更好地使用相機捕捉會話。

請參閱此文章 - 它應該可以幫助您了解您可以執行的操作: https//medium.com/androiddevelopers/understanding-android-camera-capture-sessions-and-requests-4e54d9150295

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM