仅捕获屏幕的一部分

时间:2018-09-10 07:19:46

标签: android bitmap android-camera

我想通过单击按钮仅捕获矩形中的部分并将其保存在存储器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;
       }

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

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

1 个答案:

答案 0 :(得分:0)

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

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;
}

首先,我正在为画布创建一个空的背景,其中包含我需要的尺寸(宽度高度和位图配置)。 注意:宽度和高度是原始位图的 DESIRED 切出部分的尺寸。 x y 定义了我们要从中获取该部分的位置的左上角。

enter image description here

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

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