Javaswt图像插值
Java swt image interpolation
我使用的 java 程序不是我自己写的,但我有所有的源代码。该程序由控制相机和显示图像的 SWT 小部件组成(它要复杂得多,但这是相关的部分)。到目前为止,一切都运行良好,但是从几个月前我们升级到 Maven(可能还有一些我不知道的其他升级)开始,它开始在放大时插入图像像素。这非常烦人,因为整个应用程序是取决于看到的各个像素。在升级之前,当增加像素大小时超过原始屏幕分辨率时,它会显示大的单色方块,这正是我想要的。现在它只是模糊了一切。
所以我的问题是:如何关闭图像插值?
干杯,安德烈亚斯
编辑澄清:有一个滚动条控制执行以下代码的显示图像的大小。
void zoomScaleEvent(Event event, boolean centreOnMouse){
setScale(selToScale(infoPanel.swtZoomScale.getSelection())); //force scale in range
int newScaleSel = infoPanel.swtZoomScale.getSelection();
Point dOrigin = swtImgScrollComp.getOrigin(); //display units
Rectangle clientArea = swtImgScrollComp.getClientArea();
// position in display coords of point to zoom in on
int dispZX, dispZY;
if(centreOnMouse){
dispZX = event.x;
dispZY = event.y;
}else{
dispZX = clientArea.width / 2;
dispZY = clientArea.height / 2;
}
//position in image of that point (caled using the old scale)
double lastScale = selToScale(lastScaleSel);
double imgZX = (dispZX + dOrigin.x) / lastScale;
double imgZY = (dispZY + dOrigin.y) / lastScale;
//origin of swtScrollComp of that point required to holding that point in the /same place/.
double newScale = selToScale(newScaleSel);
double newDispOX = imgZX * newScale - dispZX;
double newDispOY = imgZY * newScale - dispZY;
swtImgScrollComp.setOrigin((int)newDispOX, (int)newDispOY);
Img img = getSelectedImageFromConnectedSource();
if(img != null){
double scale[] = getScale();
swtImageCanvas.setSize(
(int)(img.getWidth()*scale[0]),
(int)(img.getHeight()*scale[1]));
}
lastScaleSel = newScaleSel;
swtImageCanvas.redraw();
}
编辑 2:我认为这是实际的绘图例程:
private void canvasPaintEvent(PaintEvent event){
Rectangle clientArea = swtImageCanvas.getClientArea();
Img image = getSelectedImageFromConnectedSource();
if(image != null && swtCurImage != null){
double scale[] = getScale();
int imgX0 = (int)((double)event.x / scale[0]);
int imgY0 = (int)((double)event.y / scale[1]);
int imgW = (int)((double)event.width / scale[0]) + 2;
int imgH = (int)((double)event.height / scale[1]) + 2;
if((imgX0 + imgW) >= image.getWidth()){
imgW = image.getWidth() - imgX0;
}
if((imgY0 + imgH) >= image.getHeight()){
imgH = image.getHeight() - imgY0;
}
try{
event.gc.drawImage(swtCurImage, imgX0, imgY0, imgW, imgH,
(int)(imgX0*scale[0]),
(int)(imgY0*scale[1]),
(int)(imgW*scale[0]),
(int)(imgH*scale[1]));
}catch(IllegalArgumentException e){
System.out.println("DRAW FAILED: " + scale + "\t" + imgX0 +", "+ imgY0+", "+ imgW+", "+ imgH +"\t-->\t" +
event.x+", "+ event.y+", "+ event.width+", "+ event.height);
}
event.gc.setForeground(swtImageCanvas.getDisplay().getSystemColor(SWT.COLOR_BLACK));
if(selectStartTime >= 0){
event.gc.drawRectangle(
(int)(selX0 * scale[0]), (int)(selY0 * scale[1]),
(int)(selW * scale[0]), (int)(selH * scale[1]));
}else{
event.gc.drawLine(0, (int)(posY * scale[1]), clientArea.width, (int)(posY * scale[1]));
event.gc.drawLine((int)(posX * scale[0]), 0, (int)(posX * scale[0]), clientArea.height);
}
for(ImagePipeController sinkController : sinkControllers.values())
if(sinkController instanceof SWTControllerInfoDraw)
((SWTControllerInfoDraw)sinkController).drawOnImage(event.gc, scale, image.getWidth(), image.getHeight(), false);
if(sourceControl != null && sourceControl instanceof SWTControllerInfoDraw){
((SWTControllerInfoDraw)sourceControl).drawOnImage(event.gc, scale, image.getWidth(), image.getHeight(), true);
}
}else{
event.gc.fillRectangle(clientArea);
if(image == null){
event.gc.drawText("null image", 0, 0);
}else if(image.isDestroyed()){
event.gc.drawText("Image Destroyed", 0, 0);
}else{
event.gc.drawText("Image OK but no swtCurImage!?!", 0, 0);
}
}
}
通话
event.gc.setInterpolation(SWT.NONE);
在 event.gc.drawImage
之前关闭图像插值。
我使用的 java 程序不是我自己写的,但我有所有的源代码。该程序由控制相机和显示图像的 SWT 小部件组成(它要复杂得多,但这是相关的部分)。到目前为止,一切都运行良好,但是从几个月前我们升级到 Maven(可能还有一些我不知道的其他升级)开始,它开始在放大时插入图像像素。这非常烦人,因为整个应用程序是取决于看到的各个像素。在升级之前,当增加像素大小时超过原始屏幕分辨率时,它会显示大的单色方块,这正是我想要的。现在它只是模糊了一切。
所以我的问题是:如何关闭图像插值?
干杯,安德烈亚斯
编辑澄清:有一个滚动条控制执行以下代码的显示图像的大小。
void zoomScaleEvent(Event event, boolean centreOnMouse){
setScale(selToScale(infoPanel.swtZoomScale.getSelection())); //force scale in range
int newScaleSel = infoPanel.swtZoomScale.getSelection();
Point dOrigin = swtImgScrollComp.getOrigin(); //display units
Rectangle clientArea = swtImgScrollComp.getClientArea();
// position in display coords of point to zoom in on
int dispZX, dispZY;
if(centreOnMouse){
dispZX = event.x;
dispZY = event.y;
}else{
dispZX = clientArea.width / 2;
dispZY = clientArea.height / 2;
}
//position in image of that point (caled using the old scale)
double lastScale = selToScale(lastScaleSel);
double imgZX = (dispZX + dOrigin.x) / lastScale;
double imgZY = (dispZY + dOrigin.y) / lastScale;
//origin of swtScrollComp of that point required to holding that point in the /same place/.
double newScale = selToScale(newScaleSel);
double newDispOX = imgZX * newScale - dispZX;
double newDispOY = imgZY * newScale - dispZY;
swtImgScrollComp.setOrigin((int)newDispOX, (int)newDispOY);
Img img = getSelectedImageFromConnectedSource();
if(img != null){
double scale[] = getScale();
swtImageCanvas.setSize(
(int)(img.getWidth()*scale[0]),
(int)(img.getHeight()*scale[1]));
}
lastScaleSel = newScaleSel;
swtImageCanvas.redraw();
}
编辑 2:我认为这是实际的绘图例程:
private void canvasPaintEvent(PaintEvent event){
Rectangle clientArea = swtImageCanvas.getClientArea();
Img image = getSelectedImageFromConnectedSource();
if(image != null && swtCurImage != null){
double scale[] = getScale();
int imgX0 = (int)((double)event.x / scale[0]);
int imgY0 = (int)((double)event.y / scale[1]);
int imgW = (int)((double)event.width / scale[0]) + 2;
int imgH = (int)((double)event.height / scale[1]) + 2;
if((imgX0 + imgW) >= image.getWidth()){
imgW = image.getWidth() - imgX0;
}
if((imgY0 + imgH) >= image.getHeight()){
imgH = image.getHeight() - imgY0;
}
try{
event.gc.drawImage(swtCurImage, imgX0, imgY0, imgW, imgH,
(int)(imgX0*scale[0]),
(int)(imgY0*scale[1]),
(int)(imgW*scale[0]),
(int)(imgH*scale[1]));
}catch(IllegalArgumentException e){
System.out.println("DRAW FAILED: " + scale + "\t" + imgX0 +", "+ imgY0+", "+ imgW+", "+ imgH +"\t-->\t" +
event.x+", "+ event.y+", "+ event.width+", "+ event.height);
}
event.gc.setForeground(swtImageCanvas.getDisplay().getSystemColor(SWT.COLOR_BLACK));
if(selectStartTime >= 0){
event.gc.drawRectangle(
(int)(selX0 * scale[0]), (int)(selY0 * scale[1]),
(int)(selW * scale[0]), (int)(selH * scale[1]));
}else{
event.gc.drawLine(0, (int)(posY * scale[1]), clientArea.width, (int)(posY * scale[1]));
event.gc.drawLine((int)(posX * scale[0]), 0, (int)(posX * scale[0]), clientArea.height);
}
for(ImagePipeController sinkController : sinkControllers.values())
if(sinkController instanceof SWTControllerInfoDraw)
((SWTControllerInfoDraw)sinkController).drawOnImage(event.gc, scale, image.getWidth(), image.getHeight(), false);
if(sourceControl != null && sourceControl instanceof SWTControllerInfoDraw){
((SWTControllerInfoDraw)sourceControl).drawOnImage(event.gc, scale, image.getWidth(), image.getHeight(), true);
}
}else{
event.gc.fillRectangle(clientArea);
if(image == null){
event.gc.drawText("null image", 0, 0);
}else if(image.isDestroyed()){
event.gc.drawText("Image Destroyed", 0, 0);
}else{
event.gc.drawText("Image OK but no swtCurImage!?!", 0, 0);
}
}
}
通话
event.gc.setInterpolation(SWT.NONE);
在 event.gc.drawImage
之前关闭图像插值。