截屏并存储图片截图

1 篇文章 0 订阅
1 篇文章 0 订阅
public static Bitmap takeScreenShot(Activity activity) {
      // View是你需要截图的View
      View view = activity.getWindow().getDecorView();
      view.setDrawingCacheEnabled(true);
      view.buildDrawingCache();
      Bitmap b = null;
      try {
         Bitmap b1 = view.getDrawingCache();
         // 获取状态栏高度
         Rect frame = new Rect();
         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
         int statusBarHeight = frame.top;
   
         int width = b1.getWidth();
 
         int height = b1.getHeight();
   
         Matrix matrix = new Matrix();

         matrix.postScale(0.5f, 0.5f);//产生缩放后的Bitmap对象

         b = Bitmap.createBitmap(b1, 0,
               statusBarHeight, width, height-statusBarHeight, matrix, true);

      } catch (Exception e) {
         b = null;
         e.printStackTrace();
      }
      view.destroyDrawingCache();
      return b;
   }


//保存签名文件
protected boolean saveBitmap(Context context) {
    try {
        // 将图片保存到所有图片文件夹中

        Bitmap bitmap = Utils.takeScreenShot(InformationActivity.this);

        RelativeLayout rel = ((RelativeLayout) sdiv_signView.getParent());
        int signPicWidth = sdiv_signView.getWidth();
        int signPicHeight = sdiv_signView.getHeight();
        int signPicMarginTop = rel.getTop();
        int signPicMarginLeft = rel.getLeft();
        LogUtil.e("bitmap w=" + bitmap.getWidth() + "\nbitmap h=" + bitmap.getHeight());

        int left = signPicMarginLeft / 2;
        int top = signPicMarginTop / 2;
        int width = signPicWidth / 2;
        int height = signPicHeight / 2;
        float wScale = (float) PRINT_WIDTH / width;
        while ((int) (height * wScale) % 8 != 0) {
            height++;
        }

        Matrix matrix = new Matrix();
        matrix.postScale(wScale, wScale);
        LogUtil.e("signPicWidth w=" + width);
        LogUtil.e("signPicHeight w=" + height);
        LogUtil.e("matrix w=" + wScale);

        Bitmap bmptemp = Bitmap.createBitmap(bitmap, left, top, width, height, matrix,
                false);

        if (bmptemp == null) {
            return false;
        } else {
            write(ConvertBmpUtil.convertBitBmp2byte(bmptemp));
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

存:
 public void write(byte[] bs) throws IOException {

        File path = new File(Constants.SIGNPATH);
        if (!path.exists()) {
            path.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(path + "/dianziqianming.bmp");

        out.write(bs);
        out.flush();
        out.close();
    }

取:

FileInputStream fis = new FileInputStream(Constants.SIGNPATH + "/" + printStr[1] + printStr[2] + "dzqm.bmp");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值