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");
// 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");