|
简单的说就是用java 实现一个图片放大缩小的 功能 由于直接位图放大 会失真 说以要尽量实现矢量缩放 网上插了下 现在比较流行的就是 双线性插值算法了 但是网上的都是c++版的 我自己实现了一个java版的 但是无法 执行 郁闷死了 一点办法都没有 我把代码贴出来 希望大家帮忙 解决 谢谢 谢谢 谢谢了
package shili;
import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.*;
import stamp.StampEdit; import java.util.ArrayList; import org.eclipse.swt.*; import org.eclipse.swt.widgets.Listener; import stamp.*; import views.Mycomposite; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import views.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.RGB;
public class zijizuo { private Shell shell; private Display display; private Mycomposite mycomposite; private static String path = ".//image/hdl01.bmp"; public static Image image; private static Image image2; private static ImageData imageData; private GC gc; private Image image1; private Rectangle clip;
// 取整参数 private static final int FP_SHIFT = 13; private static final int FP_ONE = 1 << FP_SHIFT; private static final int FP_HALF = 1 << (FP_SHIFT - 1);
public static final int MODE_POINT_SAMPLE = 0; public static final int MODE_BOX_FILTER = 1;
private Rectangle rect;
private int[] getPixels1(Image src) {
int w = rect.width; int h = rect.height; int[] pixels = new int[w * h]; src.getImageData().getPixels(0, 0, w * h, pixels, 0);
// for (int i = 0; i < pixels.length; i++) { // System.out.print(pixels); // } return pixels;
}
private Image getNewImage(int[] pixels, int w, int h) { ImageData imageDate = new ImageData(".//image/hdl01.bmp"); imageDate.setPixels(0, 0, w * h, pixels, 0); Image image = new Image(display, imageDate);
return null; }
private Image resizeImage(Image src, int destW, int destH, int mode) { int srcW = rect.width; int srcH = rect.height; int destPixels[] = new int[srcW * srcH]; int srcPixels[] = getPixels1(src); if (mode == MODE_POINT_SAMPLE) { for (int destY = 0; destY < destH; destY++) { for (int destX = 0; destX < destW; destX++) { int srcX = (destX * srcW) / destW; int srcY = (destY * srcH) / destH; destPixels[destX + destY * destW] = srcPixels[srcX + srcY * srcW]; // destPixels[destX + destY * destW] =1; // srcPixels[srcX + srcY* srcW]=1; } } } else { int ratioW = (srcW << FP_SHIFT) / destW; int ratioH = (srcH << FP_SHIFT) / destH;
int[] tmpPixels = new int[destW * srcH];
int argb; int a, r, g, b; int count;
for (int y = 0; y < srcH; ++y) { for (int destX = 0; destX < destW; ++destX) { count = 0; a = 0; r = 0; b = 0; g = 0; int srcX = (destX * ratioW) >> FP_SHIFT; int srcX2 = ((destX + 1) * ratioW) >> FP_SHIFT; do { argb = srcPixels[srcX + y * srcW]; a += ((argb & 0xff000000) >> 24); r += ((argb & 0x00ff0000) >> 16); g += ((argb & 0x0000ff00) >> 8); b += (argb & 0x000000ff); ++count; ++srcX; } while (srcX <= srcX2 && srcX + y * srcW < srcPixels.length);
a /= count; r /= count; g /= count; b /= count;
tmpPixels[destX + y * destW] = ((a << 24) | (r << 16) | (g << 8) | b); for (int x = 0; x < destW; ++x) { for (int destY = 0; destY < destH; ++destY) { count = 0; a = 0; r = 0; b = 0; g = 0; int srcY = (destY * ratioH) >> FP_SHIFT; int srcY2 = ((destY + 1) * ratioH) >> FP_SHIFT; do { argb = tmpPixels[x + srcY * destW]; a += ((argb & 0xff000000) >> 24); r += ((argb & 0x00ff0000) >> 16); g += ((argb & 0x0000ff00) >> 8); b += (argb & 0x000000ff); ++count; ++srcY; } while (srcY <= srcY2 && x + srcY * destW < tmpPixels.length);
a /= count; a = (a > 255) ? 255 : a; r /= count; r = (r > 255) ? 255 : r; g /= count; g = (g > 255) ? 255 : g; b /= count; b = (b > 255) ? 255 : b;
destPixels[x + destY * destW] = ((a << 24) | (r << 16) | (g << 8) | b); } } }
}
}
return getNewImage(destPixels, destW, destH); }
private void doZoom() {
}
private void open() { display = new Display(); shell = new Shell(display); shell.setLayout(new FillLayout()); Canvas canvas = new Canvas(shell, SWT.PUSH); Canvas canvas1= new Canvas(shell, SWT.BORDER); image = new Image(canvas.getDisplay(), path); rect = image.getBounds(); imageData=image.getImageData().scaledTo(rect.width,rect.height); image2=new Image(display,imageData); // rect = image.getBounds(); // getPixels1(image); // image2 = resizeImage(image, rect.width / 2, rect.height / 2, 0); PaletteData palette=new PaletteData(0,0,0); palette.colors=new RGB[]{new RGB(0,0,0),new RGB(255,255,255)}; palette.isDirect=false; ImageData wmBuf1=new ImageData(100,100,1,palette); byte[] data1=wmBuf1.data; byte[] data2=new byte [1000*1000]; int m=rect.width*rect.height; for(int i=0;i<m;i++){ int k=0; int y=0; if(k==m){ k=0;} if(k<101){ data1[y]=data2; y++; } k++; } // wmBuf1.data=data1; wmBuf1.setPixels(0, 0, 10000, data1, 0); image1=new Image(display,wmBuf1); image1=new Image(display,100,100); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { gc=e.gc; gc.drawImage(zijizuo.image2, 0, 0); gc.setClipping(10, 10, 100, 100); gc.drawFocus(10, 10, 100, 100); // gc.drawImage(image1,0,0,115,164,5,5,230,82);
// clip = gc.getClipping(); // gc.drawImage(image2, 10, 10, 100, 100, // 10, 10, 100, 100); } }); // Rectangle clip = gc.getClipping();
canvas1.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawImage(image1, 0, 0); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
public static void main(String age[]) { new zijizuo().open(); } }
程序些的很烂 对付看吧
|