截取指定区域的并保存图片

  1. view.setDrawingCacheEnabled(true);   
  2. view.buildDrawingCache();   
  3. bitmap = view.getDrawingCache();  
然后我们需呀计算出我们选定区域的坐标点,注意正选和反选的计算方式不同 ,
  1. public boolean onTouch(View v, MotionEvent event) {   
  2.      if(event.getAction() == MotionEvent.ACTION_DOWN){   
  3.             x = 0;   
  4.             y = 0;   
  5.             width = 0;   
  6.             height = 0;   
  7.             x = (int) event.getX();   
  8.             y = (int) event.getY();   
  9.         }   
  10.       if(event.getAction() == MotionEvent.ACTION_MOVE){   
  11.             m = (int) event.getX();   
  12.             n = (int) event.getY();   
  13.             myView.setSeat(x, y, m, n);   
  14.             myView.postInvalidate();   
  15.         }   
  16.       if(event.getAction() == MotionEvent.ACTION_UP){   
  17.     if(event.getX()>x){   
  18.          width = (int)event.getX()-x;   
  19.     }else{   
  20.          width = (int)(x-event.getX());   
  21.           x = (int) event.getX();   
  22.     }   
  23.                  if(event.getY()>y){   
  24.          height = (int) event.getY()-y;   
  25.      }else{   
  26.          height = (int)(y-event.getY());   
  27.           y = (int) event.getY();   
  28.     }   
  29.     image2.setImageBitmap(getBitmap(this));   
  30.         }   
  31.     if(myView.isSign()){   
  32.          return false;   
  33.     }else{   
  34.           return true;   
  35.     }   

然后为我们计算出来的坐标区域添加选中效果

  1. protected void onDraw(Canvas canvas) {   
  2.          if(sign){   
  3.     paint.setColor(Color.TRANSPARENT);   
  4.          }else{   
  5.     paint.setColor(Color.RED);   
  6.     paint.setAlpha(80);   
  7.     canvas.drawRect(new Rect(x, y, m, n), paint);   
  8.            }   
  9.     super.onDraw(canvas);   
  10. }  

最后生成我们需要的图片展示出来,顺便保存到SD卡下一张。

  1. Rect frame = new Rect();   
  2.       activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);   
  3.       int toHeight = frame.top;   
  4.       bitmap = Bitmap.createBitmap(bitmap, x, y+2*toHeight, width, height);   
  5.       try {   
  6.     FileOutputStream fout = new FileOutputStream("mnt/sdcard/test.png");   
  7.     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);   
  8.         } catch (FileNotFoundException e) {   
  9.             // TODO Auto-generated catch block   
  10.             e.printStackTrace();   
  11.         }   
  12.         view.setDrawingCacheEnabled(false);
要批量截取指定区域保存图片,您可以使用OpenCV库来完成。以下是一个示例代码,演示了如何从多个图像截取指定区域保存。 ```python import cv2 import os # 定义要截取区域坐标 x = 100 # 左上角 x 坐标 y = 100 # 左上角 y 坐标 width = 200 # 区域宽度 height = 200 # 区域高度 # 定义输入图像文件夹路径和输出图像文件夹路径 input_folder = 'path/to/input/folder' output_folder = 'path/to/output/folder' # 获取输入文件夹中的所有图像文件列表 image_files = [f for f in os.listdir(input_folder) if os.path.isfile(os.path.join(input_folder, f))] # 遍历每个图像文件 for image_file in image_files: # 读取图像 image_path = os.path.join(input_folder, image_file) image = cv2.imread(image_path) # 截取指定区域 roi = image[y:y+height, x:x+width] # 构造输出文件路径 output_file = os.path.splitext(image_file)[0] + '_cropped.jpg' output_path = os.path.join(output_folder, output_file) # 保存截取后的图像 cv2.imwrite(output_path, roi) print(f"已保存截取后的图像: {output_file}") print("批量截取保存完成") ``` 请确保将代码中的 `'path/to/input/folder'` 替换为实际的输入图像文件夹路径,将 `'path/to/output/folder'` 替换为实际的输出图像文件夹路径。代码将遍历输入文件夹中的所有图像文件,读取每个图像截取指定区域,然后将截取后的图像保存到输出文件夹中。最后,将会打印出已保存截取后的图像文件名。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值