仅捕获部分屏幕

问题描述 投票:0回答:1

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

我面临的问题是,在某些手机上,该部分拍摄的图像的清晰度非常糟糕,就像它模糊一样,在某些手机中图像被保存为旋转,有人可以帮我,是否有任何变化我应该做或有没有其他方法来有效地做到这一点。

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

android bitmap android-camera
1个回答
0
投票

我正在使用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;
}

首先,我正在为画布创建一个空背景,我需要测量(宽度,高度和位图配置)。注意:宽度和高度是orginial位图的DESIRED cutted部分的度量。 x和y定义左上角,我们要从中获取该部分。

enter image description here

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

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

© www.soinside.com 2019 - 2024. All rights reserved.