直接上代码了,
var img_this=new Image();
img_this.src=$('#preview').attr('src');
var width = img_this.width,height = img_this.height;
var scale = width / height;
width1 = 300;
height1 = parseInt(width1 / scale);
var canvas = $("#cans");
canvas[0].width = width1; canvas[0].height = height1;
var cropStr =canvas[0].toDataURL("image/jpeg");
alert(cropStr);
这是生成的base64的字符串,
public boolean strtoimg(String imgStr){
if (imgStr == null){ // 图像数据为空
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
String path = "d:555.jpg";
OutputStream out = new FileOutputStream(path);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
这个是生成图片方法,哪里有不对的吗?生成的图片是一片黑。
在线等~