如何从Camera ImageReader裁剪特定区域?

时间:2019-12-23 07:27:09

标签: android

enter image description here我正在制作一个应用,并被裁剪掉。从图像读取器获取图像时,位图没有裁剪出确切的区域,并且想知道图像设置裁剪方法的目的是什么 “ image.setCropRect(rect1)”以及位图创建方法如何裁剪图像....................... 图像宽度= 720 图像高度= 1440

@Override
public void onImageAvailable(ImageReader reader) {
//   Image image= reader.acquireNextImage();


    mBackground.post(new ImageSaver(reader.acquireLatestImage(),file,mRotation,context));



    }




   //   Another Class Process Code
       byte[] bytes;
       public int left=0;
      public int top=0;
      public int right=200;
      public int bottom=200;
      Rect rect;
      public  ImageSaver(Image image, File file , int rotation , Context context) {
    this.image= image;
   mFile = file;
 this.rotation=rotation;



   this.context=context;
    rect=  new Rect(left,top,right,bottom);
        }


@Override
public void run() {

    ByteBuffer buffer=null;
    buffer = image.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[buffer.capacity()];
    buffer.get(bytes);


    Log.i(TAG, "run: "+rect1.right);


      Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
      Bitmap bitmap1= Bitmap.createScaledBitmap(bitmap,image.getWidth(),image.getHeight(),false);
      Matrix matrix = new Matrix();
      matrix.postRotate(90);
      Bitmap bitmap2=           Bitmap bitmap2= Bitmap.createBitmap(bitmap1,left,top,right,bottom ,matrix ,false);

     ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream);
      byte[] byteArray = stream.toByteArray();




      File Path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
      File file = new File(Path, "picok.jpg");
      FileOutputStream output = null;
    try {
        output = new FileOutputStream(file);
       output.write(byteArray);

        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            image.close();
        }
        if (null != output) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}


}

0 个答案:

没有答案