left = 142, top = 80, width = 568, height = 320 ---- Растровое изображение imageCropped = Bitmap.createBitmap (imageOriginal, left, top, width, height, null, false); в этом операторе вылетает с ошибкой-- java.lang.IllegalArgumentException: x + width должно быть <= bitmap.width ()

аю приложение на основе камеры, где я помещаю прямоугольный вид поверх камеры. Когда я снимаю изображение с помощьюnew Camera.PictureCallback()Я обрезал это изображение так, чтобы оно получило часть прямоугольника. Ну, это работает нормально.

Сейчас я реализовалView.OnTouchListener и с помощью этого я сделал форму подвижной.

Итак, мне нужно сделать снимок с окончательным выбором пользователя, например, где они размещают прямоугольник.

Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440
                float scale = 1280 / 1000;
                int left = (int) (scale * (imageOriginal.getWidth() - 250) / 2);
                int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);
                int width = (int) (scale * 750);
                int height = (int) (scale * 616);
                Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);

Это метод, который я использовал для обрезки изображения. Значения жестко связаны, чтобы найти точное положение. Теперь мне нужны значения для этого верха, низа, высоты, ширины с изменяющимся прямоугольником.

// Мой customView, который использовал для рисования этого прямоугольника

public class CustomView extends View  {
    private Paint paint = new Paint();
    public CustomView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) { // Override the onDraw() Method
        super.onDraw(canvas);

        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(10);

        //center
        int x0 = canvas.getWidth()/2;
        int y0 = canvas.getHeight()/2;
        int dx = canvas.getHeight()/3;
        int dy = canvas.getHeight()/3;
        //draw guide box
        canvas.drawRect(x0-dx, y0-dy, x0+dx, y0+dy, paint);
    }


}

// код обратного вызова моей картинки

  Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {

            // Replacing the button after a photho was taken.


            // File name of the image that we just took.
            fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()).toString() + ".jpg";

            // Creating the directory where to save the image. Sadly in older
            // version of Android we can not get the Media catalog name
            File mkDir = new File(sdRoot, dir);
            mkDir.mkdirs();

            // Main file where to save the data that we recive from the camera
            File pictureFile = new File(sdRoot, dir + fileName);

            // Cropping image with the corresponding co-ordinates and save in to a file
            try {

                Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440
                float scale = 1280 / 1000;
                int left = (int) (scale * (imageOriginal.getWidth() - 250) / 2);
                int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);
                int width = (int) (scale * 750);
                int height = (int) (scale * 616);
                Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);

                FileOutputStream purge = new FileOutputStream(pictureFile);
                imageConverted.compress(Bitmap.CompressFormat.JPEG, 100, purge);
                purge.flush();
                purge.close();

            } catch (FileNotFoundException e) {
                Log.d("DG_DEBUG", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
            }

            // Adding Exif data for the orientation.
            try {
                ProjectManager.getInstance().settings.IMAGE_LOCATION = "/sdcard/" + dir + fileName;
                exif = new ExifInterface(ProjectManager.getInstance().settings.IMAGE_LOCATION);
                exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation);
                exif.saveAttributes();
                mView.saveImage(dir + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    };

Ответы на вопрос(2)

Ваш ответ на вопрос