📄 imageproxy.java
字号:
package net.sf.component.simplenote;
import java.io.File;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.PaintObjectEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Composite;
/**
* 定义图片信息
* 也可由形如<img src=aaa.jpg width=120 height=160></img>的字串构成
* @author levin
*/
public class ImageProxy extends ElementProxy {
private ImageData imageData;
public ImageProxy(String src, int width,int height) {
super();
this.src = src;
this.width = width;
this.height = height;
}
public ImageProxy(ImageData imageData) {
super();
this.imageData = imageData;
this.width = imageData.width;
this.height = imageData.height;
}
public ImageProxy(String html){
//防止解析出错
this("",10,10);
try{
Pattern p=Pattern.compile("<img\\s+src=([a-z\\.A-Z/0-9]*)\\s+width=([0-9]*)\\s+height=([0-9]*)></img>");
Matcher m=p.matcher(html);
if(m.find()){
this.src=m.group(1);
this.width=Integer.parseInt(m.group(2));
this.height=Integer.parseInt(m.group(3));
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public ImageProxy() {
}
//取定义的html
@Override
public String getHtml(){
return "<img src="+src+" width="+width+" height="+height+"></img>";
}
public int[] getMetrics(){
return new int[]{height,0,width};
}
public void save(String basePath){
try {
File imageFile=createEmbedFile(basePath,".png");
ImageLoader loader=new ImageLoader();
loader.data=new ImageData[]{getImageData(basePath)};
loader.save(new FileOutputStream(imageFile), SWT.IMAGE_PNG);
//设置imageProxy的src属性
src="embed/"+imageFile.getName();
} catch (Exception e) {
e.printStackTrace();
}
}
public void draw(String basePath,PaintObjectEvent event,Composite parent){
Image image = new Image(parent.getDisplay(),getImageData(basePath));
int x = event.x;
int y = event.y + event.ascent - event.style.metrics.ascent;
event.gc.drawImage(image, x, y);
image.dispose();
}
public void dispose(){
}
public String getPlaceholder(){
return "\uFFFC";
}
private ImageData getImageData(String basePath) {
if(imageData == null && src != null){
try{
imageData=new ImageData(basePath+"/"+src);
}catch(Exception ex){
//图片不存在
imageData=new ImageData(width,height,8,new PaletteData(0xFF0000, 0xFF00, 0xFF));
imageData.transparentPixel=0x00;
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
//绘制边框
if(i==0 || j==0 || i==width-1 || j== height-1 )
imageData.setPixel(i, j, 0xff);
else
imageData.setPixel(i, j, 0x00);
}
}
//用对称直线画法,绘制一条对角线
{
int x1 = 0; // 起点x
int y1 = 0; // 起点y
int x2 = width-1; // 终点x
int y2 = height-1; // 终点y
int dx = x2 - x1;
int dy = y2 - y1;
int dx2 = dx << 1;
int dy2 = dy << 1;
int e = dy2 - dx; // 决策量
int half = (dx + 1) >> 1;
for (int i = 0; i < half; i++)
{
imageData.setPixel(x1, y1, 0xff);
imageData.setPixel(x2, y2, 0xff);
if (e > 0) // 当e>0时,始端y向加1,末端y向减1
{
y1++;
y2--;
e -= dx2;
}
//始端x向加1,末端x向减1
x1++;
x2--;
e += dy2;
}
}
}
}
return imageData;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -