【问题标题】:How to crop part of image file in android?如何在android中裁剪部分图像文件?
【发布时间】:2017-03-30 12:23:22
【问题描述】:

我使用相机 API2 编写了一个相机应用程序。 我可以从具体的东西上拍一张照片。然后我知道我的图片中的特定位置有重要信息(我不需要选择重要区域,我已经知道哪个部分是关键的)。我只想拥有我的图片的重要部分并将这部分用于我的 OCR 应用程序。 我的问题是如何分离图片的特定部分。有人建议我使用 com.android,camera.action.CROP 裁剪拍摄的照片。但是我在很多地方读到,这种方法可能不适用于所有安卓设备。 有谁知道我该怎么做? android支持这样的动作吗?? 在保存图片之前可以裁剪字节文件吗? 或者我应该使用像 Open-CV 这样的第三方库来做这些事情。 在此先感谢

【问题讨论】:

  • 正是您想要务实地裁剪图像??

标签: android opencv crop camera-api


【解决方案1】:

如果您有起点 x 和 y 以及图像的高度和宽度 或者你有四个点来形成一个矩形来复制图像,那么你可以使用这段代码

   private void cropBitmap1(int startX, int startY, int width, int height, Bitmap bmp) {
// widht=x+x2 height=y+Y2
    Bitmap source = bmp;
    //Log.w("TAG","widths "+widths +" width"+width+"  heights"+heights+"  height"+height+" x"+startX +" y"+startY);
    Bitmap resized = Bitmap.createBitmap(source, startX, startY, width, height);
 //save the image or execute for ocr using tesseract
  }

如果你不知道这四个点,那么就去 Opencv,这样你就可以使用训练文件来获得这些点

【讨论】:

    【解决方案2】:

    如果你想使用库,我会推荐 theartofdev 库。将此依赖项放入 app gradle 中

    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
    

    图片选择

          CropImage.activity().start(getContext(), this);
    

    这将重定向到打开或捕获图像。用户可以对其进行裁剪,然后可以通过以下方式轻松获得结果

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == -1) {
                Uri resultUri = result.getUri();
                // TODO your stuff here. Cropped image will come here as resultant
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        }
    }
    

    这是最简单的方法。您可以在此处裁剪任何图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2015-02-02
      • 2021-07-24
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多