Android裁剪图像使用rest api存储在数据库中

时间:2017-07-24 11:42:58

标签: android rest cropimage.net

当我没有应用裁剪活动时,我有一堆代码正常工作 但我想将裁剪应用于所选图像并使用Rest API将其发送到服务器

TypedFile typedFile = new TypedFile("multipart/form-data",savedFileDestination);
initiateProgressDialog();

如何设置裁剪的URI而不是savedFileDestination,以便将裁剪后的图像路径作为文件?

3 个答案:

答案 0 :(得分:0)

使用以下方法裁剪:

public static Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
    Bitmap bm = null;

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}



public static int calculateInSampleSize(

        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }

    return inSampleSize;
}

答案 1 :(得分:0)

谢谢你的所有答案和评论我发现我的解决方案是我必须首先将我的褶皱图像存储到文件中,因为歪斜的图像存储在缓存中,我们需要文件路径并将文件路径发送到服务器存储它.....再次感谢

答案 2 :(得分:0)

点击相机

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File outPutFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "Path of your custom directory);
                if (!outPutFile.exists()) {
                    outPutFile.mkdirs();
                }
                Uri capturedImageUri = Uri.fromFile(File.createTempFile("Your app directory name" + System.currentTimeMillis(), ".jpg", outPutFile));
                Logg.e(getClass().getSimpleName(), "Captured_Pic ===== " + Uri.fromFile(outPutFile));
                intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
                startActivityForResult(intent, Util.REQUEST_CAMERA);

点击图库

CropImage.startPickImageActivity(HomeActivity.this);

<强> OnActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_CANCELED) {
        switch (requestCode) {
            case Util.REQUEST_CAMERA: // Camera request
                startCropImageActivity(capturedImageUri);
                break;

            case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE: // Crop
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                try {
                    if (resultCode == RESULT_OK) {
                        resultUri = result.getUri();
                        mProfileView.setImageURI(Uri.parse(resultUri.toString())); // this is my imageview, where I'll set that cropped image Uri.
                    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                        Exception error = result.getError();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE: // Gallery request
                try {
                    Uri selectedImageUri = CropImage.getPickImageResultUri(this, data);
                    startCropImageActivity(selectedImageUri);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}

此方法会根据您的要求将属性设置为裁剪图像工具

private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setCropShape(CropImageView.CropShape.RECTANGLE)
            .setActivityMenuIconColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .setGuidelinesColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .setScaleType(CropImageView.ScaleType.FIT_CENTER)
            .setFixAspectRatio(true)
            .setBorderCornerColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .setBorderLineColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .start(this);
}