87,923
社区成员
发帖
与我相关
我的任务
分享
var new_canvas = document.createElement( "canvas" );
new_canvas.setAttribute( "id", "img_resizer" );
var thisbody = document.getElementsByTagName( "body" )[ 0 ];
thisbody.appendChild( new_canvas );
var canvas = document.getElementById( "img_resizer" );
var ctx = canvas.getContext( "2d" );
var imgs = document.getElementsByTagName( "img" );
for( var i = 0; i < imgs.length; i++ ) {
var MAX_WIDTH = 500;
var MAX_HEIGHT = 500;
var width = imgs[ i ].width;
var height = imgs[ i ].height;
if( width > height ) {
if( width > MAX_WIDTH ) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if( height > MAX_HEIGHT ) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
ctx.drawImage( imgs[ i ], 0, 0, width, height );
var dataURL = canvas.toDataURL( "image/jpeg", 0.1 );
imgs[ i ].setAttribute( "src", dataURL );
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo JavaScript PNG Viewer</title>
</head>
<body onload="show(gravatar);">
<script src="../Source/Base64.js" type="text/javascript"></script>
<script src="../Source/Deflate.js" type="text/javascript"></script>
<script src="../Source/PNG.js" type="text/javascript"></script>
<script type="text/javascript">
var gravatar = 'iVBORw0KGgoAAAANSUhEUgAAA.......数据从略......55CYII=';
String.prototype.padRight = function(c, n){
var txt = '';
for(var i=0;i<n-this.length;i++) txt += c;
return txt + this;
};
function show(data){
var png = new PNG(data);
var img = document.getElementById('image'), limg = document.getElementById('largeimage');
document.getElementById('nativeimage').src = 'data:image/png;base64,' + data;
img.innerHTML = '';
limg.innerHTML = '';
img.style.width = png.width + 'px';
img.style.height = png.height + 'px';
limg.style.width = (png.width * 3) + 'px';
limg.style.width = (png.height * 3) + 'px';
var line;
while(line = png.readLine())
{
for (var x = 0; x < line.length; x++){
var px = document.createElement('div'), px2 = document.createElement('div');
px.className = px2.className = 'pixel';
px.style.backgroundColor = px2.style.backgroundColor = '#' + line[x].toString(16).padRight('0', 6);
img.appendChild(px);
limg.appendChild(px2);
}
}
}
</script>
<div id="image"></div>
<div id="largeimage"></div>
<img id="nativeimage" />
</body>
</html>
<html>
<head>
<title>Hello,World</title>
</head>
<body>
<img src="data:image/png;base64,${encodeString}" />
</body>
</html>
public class Base64ImageAction extends ActionSupport {
private final static String galleryName = "gallery";
private static String parent = null;
private String encodeString = null;
public String getEncodeString() {
return encodeString;
}
public void setEncodeString(String encodeString) {
this.encodeString = encodeString;
}
private String getImageFullPath() {
parent = new File(this.getClass().getClassLoader().getResource(
File.separator).getPath()).getParent()+File.separator+"flag.jpg";
}
public String execute() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
JimiReader reader = Jimi.createJimiReader(this.getImageFullPath());
Image image = reader.getImage();
Jimi.putImage("image/png", image, output);
output.flush();
output.close();
this.encodeString = Base64.encodeBase64String(output.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} catch (JimiException e) {
e.printStackTrace();
}
return SUCCESS;
}
}