
如果是要将一个Bitmap绘制到界面上,那就直接用Canvas#drawBitmap()方法绘制即可
如果是要从一个已有的canvas获取bitmap对象,可尝试下面的办法:
1、用BitmapcreateBitmap()方法,创建一个与目标canvas大小相同的空白Bitmap对象
2、用Canvas#setBitmap()方法,将创建的bitmap设置为Canvas对象的Bitmap。之后所有对目标canvas的绘制 *** 作都会绘制在这个bitmap上,相当于将canvas中的内容转换为了一个单独的bitmap
另外,如果创建canvas对象的时候调用的是canvas(Bitmap bitmap)方法的话,那么创建canvas的时候就应该已经有现存的bitmap对象了,这种情况应该不需要转换
你的问题是想怎么取出数据还是怎么输出到画布?
画布有textout方法,可以在指定位置输出文本
Image1CanvasBrushColor
:=
clBlack;
Image1CanvasFillRect(Image1ClientRect);
Image1CanvasFontColor
:=
clRed;
Image1CanvasFontName
:=
'Times
New
Roman';
Image1CanvasFontSize
:=
18;
Image1CanvasTextOut(9,
36,
'Delphi');
找到两个前端就能解决的方法,最后因为各种原因采用了方法二。
方法一:
找到地图上的全部点,然后在canvas上面重绘一次。
html2canvas(this$refstarget, {
useCORS: true, // 如果截图的内容里有,可能会有跨域的情况,加上这个参数,解决文件跨域问题
})then((canvas) => {
let cans = canvasgetContext("2d");
//批量地图重新打点 加载
documentquerySelectorAll("#mapView_layers image")forEach((item) => {
var obj = item;
var x = itemgetAttribute("x");
var y = itemgetAttribute("y");
var itemWidth = itemgetAttribute("width");
var itemHeight = itemgetAttribute("height");
consolelog("item", item, x, y);
if (width == 8) {
cansdrawImage(obj, x, y, itemWidth, itemHeight);
} else {
cansdrawImage(
obj,
x ,
y - 1 - itemHeight / 2 ,
itemWidth,
itemHeight
);
}
});
//下面是截图代码
})
登录后复制
因为本身目标dom的position定位问题,最后打的点可能会出现偏移。
所以还要给html2canvas加几个属性: x , y , scrollX , scrollY。保险起见,再加上两个参数 width 和 height 。
本人是后面chrome测着没问题,但是给小伙伴测试的时候,他用的360浏览器还有个xx浏览器有点问题。干脆参数全加上。
screenShot() {
let canvasBox = this$refstarget;
//获取目标div位置;
var tPosition = canvasBoxgetBoundingClientRect();
consolelog("size", tPosition);
// 获取父级的宽高
const width = parseInt(windowgetComputedStyle(canvasBox)width);
const height = parseInt(windowgetComputedStyle(canvasBox)height);
html2canvas(this$refstarget, {
width: width,
height: height,
x: 0,
y: 0,
scrollY: -tPositiony,
scrollX: -tPositionx,
useCORS: true, // 如果截图的内容里有,可能会有跨域的情况,加上这个参数,解决文件跨域问题
})then((canvas) => {
})
}
登录后复制
要是项目的地图是不可移动的,基本到这里就可以了。
但是地图只要一挪动。。一个新的bug出现了。。。。。整个地图画线打点层的偏移量和截图之前不一样。。。。 截图后,画线层偏的比原地图还要远,打点却还在原位没动过。。
这个问题需要修正svg的偏移,然后这个标注点绘制的时候也要加上一个偏移量。
地图偏移的bug后面再讲。
方法二:(最后采用)
把svg中所有的<image>的href路径转换为base64编码格式。简单方便,不用考虑位置什么的问题,就是有些浏览器里面加载慢。。。setTimeout有时候要设置大一点。。
screenShot() {
let canvasBox = this$refstarget;
//获取目标div位置;
var tPosition = canvasBoxgetBoundingClientRect();
consolelog("size", tPosition);
// 获取父级的宽高
const width = parseInt(windowgetComputedStyle(canvasBox)width);
const height = parseInt(windowgetComputedStyle(canvasBox)height);
//---------------------
//解决svg 内部image加载不了的问题,把image改为base64,配合setTimeout html2canvas使用
documentquerySelectorAll("#mapView_layers image")forEach((item) => {
consolelog("item", item);
var img = itemgetAttribute("xlink:href");
consolelog("href", img);
var image = new Image();
imagecrossOrigin = "";
imagesrc = img;
imageonload = () => {
var base64 = getBase64Image(image);
itemsetAttribute("xlink:href", base64); //更改href属性
};
});
//地址转为base64编码
function getBase64Image(img) {
var canvas = documentcreateElement("canvas");
canvaswidth = imgwidth;
canvasheight = imgheight;
var ctx = canvasgetContext("2d");
ctxdrawImage(img, 0, 0, imgwidth, imgheight);
var ext = imgsrcsubstring(imgsrclastIndexOf("") + 1)toLowerCase();
var dataURL = canvastoDataURL("image/" + ext);
return dataURL;
}
setTimeout(() => {
html2canvas(this$refstarget, {
width: width,
height: height,
x: 0,
y: 0,
scrollY: -tPositiony,
scrollX: -tPositionx,
useCORS: true, // 如果截图的内容里有,可能会有跨域的情况,加上这个参数,解决文件跨域问题
})then((canvas) => {
})
}, 200);
}
登录后复制
以上就是关于如何将Canvas 上的内容转换为一张Bitmap-Android开发问答全部的内容,包括:如何将Canvas 上的内容转换为一张Bitmap-Android开发问答、delphi如何提取数据库中的内容输出到画布canvas中、gis多个图层地图用htmlcanvas截图获取不到等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)