【问题标题】:Capturing only part of screen只截取屏幕的一部分
【发布时间】:2019-02-14 14:20:45
【问题描述】:

我想通过单击按钮仅捕获矩形中的部分并将其保存在存储中See Image Here我正在创建一个应用程序,背景是相机预览,中心存在一个矩形(矩形由在矩形周围创建四个布局并将它们的背景颜色设置为部分透明,这样当我单击捕获图像按钮时,它看起来就像添加了一个叠加层,它捕获了整个屏幕预览的图像,但我只想要存在的部分图像在矩形中,这是我尝试过的,

captuteimageonpro.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               camera.takePicture(null, null, mPictureCallback);
           }
       });


Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
       @Override
       public void onPictureTaken(byte[] data, Camera camera) {

           BitmapFactory.Options options = new BitmapFactory.Options();
           options.inScaled = false;
           Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

           Display display = getWindowManager().getDefaultDisplay();
           Point size = new Point();
           display.getSize(size);
           int width = bitmap.getWidth();
           int height = bitmap.getHeight();

           int start_width = (int) (width * 0.15);
           int start_height = (int) (height * 0.16);
           int end_width = start_width + (int) (width * 0.70);
           int end_height = start_height + (int) (height * 0.52);
           //the decimal values in above lines are the percentages of the rectangle position relative to screen

           int no_pixels = (end_width - start_width) * (end_height - start_height);
           int[] pixels = new int[no_pixels];
           ByteArrayOutputStream bos = new ByteArrayOutputStream();

           bitmap.getPixels(pixels, 0, (end_width - start_width), start_width, start_height, (end_width - start_width), (end_height - start_height));

           bitmap = Bitmap.createBitmap(pixels, 0, (end_width - start_width), (end_width - start_width), (end_height - start_height), Bitmap.Config.ARGB_8888);

           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
           byte[] byteArray = stream.toByteArray();
           bitmap.recycle();

           File picture_file = getOutputMediaFile();
           if(picture_file == null)
           {
               return;
           }
           else {
               try {
                   FileOutputStream fos = new FileOutputStream(picture_file);
                   fos.write(byteArray);

                   fos.close();

                   camera.startPreview();


               }catch (FileNotFoundException e)
               {
                   e.printStackTrace();
               }catch(IOException e)
               {
                   e.printStackTrace();
               }
           }
       }
   };



private File getOutputMediaFile()
   {
       String state = Environment.getExternalStorageState();
       if(!state.equals(Environment.MEDIA_MOUNTED))
       {
           return null;
       }
       else
       {
           File folder_gui = new File(Environment.getExternalStorageDirectory()+ File.separator+"GUI");

           if(!folder_gui.exists())
           {
               folder_gui.mkdirs();
           }
           File outputFile = new File(folder_gui,"temp.jpg");
           return outputFile;
       }

我面临的问题是,在某些手机上,在该部分捕获的图像的清晰度非常差,就像模糊一样,在某些手机中,图像是旋转保存的,有人可以帮我吗?我应该做的任何改变,或者有没有其他方法可以有效地做到这一点。

我不想手动裁剪图像,只需单击按钮后,矩形中存在的相机预览部分应该保存在存储中。

【问题讨论】:

  • Crop image in android的可能重复
  • @Srijay 请让我们知道所提供的答案是否适合您
  • @ThomasRichter 感谢您的回答,但它没有解决我的问题,请检查我添加到您的回答中的评论

标签: android bitmap android-camera


【解决方案1】:

出于这个原因,我正在使用 Canvas。基本上我用我需要的测量值创建了一个空画布,并将位图的选定部分绘制到画布上:

private Bitmap cropBitmap(Bitmap bitmap, int x, int y, int width, int height) { 
    Bitmap defaultBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
    Canvas canvas = new Canvas(defaultBitmap); 
    canvas.drawBitmap(bitmap, new Rect(x,y,width,height), new Rect(0,0,width,height), new Paint());
    return bitmap;
}

首先,我为画布创建了一个空白背景,其中包含我需要的测量值(widthheight 和位图配置)。 注意:宽度和高度是原始位图 DESIRED 切出部分的测量值。 xy 定义了左上角,我们希望从中获取部分。

第一个 Rect 选择位图的一部分,我们需要将其绘制到画布上。 第二个 Rect 指定裁剪图像在画布上的位置和大小(本例中为全尺寸)。

您还可以在此处查看画布文档:https://developer.android.com/reference/android/graphics/Canvas#Canvas(android.graphics.Bitmap)

【讨论】:

  • 我已经尝试过你所说的,整个预览被保存在存储中,而不是矩形中的部分,你提到的变量 x,y,width,height 是相对于 bitmap.width()和 bitmap.height() 对吗?
  • @Srijay:我已经更新了我的答案,以阐明应该如何使用该函数。宽度和高度不是位图的宽度和高度,而是需要从位图中切出的部分的宽度和高度。 x 和 y 指定原始位图的坐标,从哪里开始切割。
  • 问题解决了,图片模糊是因为相机的图片尺寸,我修改了相机的参数来设置图片尺寸。现在图像很清晰。谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多