in this block of code iam displaying the magnification of the
original image i want to smooth the image means squares to be removed
while magnification i tried this but id idnt, get the smooth any body
can help me thanks
here the displayable image is the original image from where the
subimage is drawn
public void magnify() {
try{
float weight = 1.0f/9.0f;
float[] elements = new float[9]; // create 2D array
// fill the array with nine equal elements
for (int i = 0; i < 9; i++) {
elements[i] = weight;
}
// use the array of elements as argument to create a Kernel
Kernel myKernel = new Kernel(3, 3, elements);
ConvolveOp simpleBlur = new ConvolveOp(myKernel);
/*int zx=zoomx;
int zy=zoomy;
if (zoomx < -1684) zx = -1684;
if (zoomy < -1684) zy = -1684;*/
int xDiff = 0,yDiff = 0,wDiff = 0,hDiff =
0;
int imageX1 = (this.getWidth() -
displayableImage.getWidth(this) + zoomx) / 2 + this.imageX;
int imageY1 = (this.getHeight() -
displayableImage.getHeight(this) + zoomy) / 2 + this.imageY;
scaleWidth = displayableImage.getWidth
(this) - zoomx;
scaleHeight = displayableImage.getHeight
(this) - zoomy;
Graphics2D sog =
displayableImage.createGraphics();
qualityRequested = 1;
sog.setRenderingHint
(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
sog.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//setRenderingHints(sog,
qualityRequested);
//synchronized (this) {
Graphics g = getGraphics();
Graphics2D gre1 = (Graphics2D)g;
//qualityRequested = 1;
gre1.setRenderingHint
(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
gre1.setRenderingHint
(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//setRenderingHints(gre1,
qualityRequested);
Rectangle magRect = null;
Image tmpimg = null;
int imgWidth = 0;
int imgHeight = 0;
float magArea = 0;
magArea = (int) (LENS_SIZE /
zoomFactor);
magRect = new Rectangle((int)
(curDragX - imageX1 - magArea / 2), (int) (curDragY - imageY1 -
magArea / 2), (int) (magArea), (int) (magArea));
Rectangle tempRect = new
Rectangle(0, 0, 0, 0);
if (scaledOriginalImage != null)
{
imgWidth =
scaledOriginalImage.getWidth(null);
imgHeight =
scaledOriginalImage.getHeight(null);
tempRect.x = 0;
tempRect.y = 0;
tempRect.width =
imgWidth;
tempRect.height =
imgHeight;
if (!magRect.intersects
(tempRect)) {
paintStatus(g);
paintArrows(g);
if
(bFirstTime1) {
bFirstTime1 = false;
repaint();
}
return;
} else {
bFirstTime1 =
true;
}
if (magRect.x < 0) {
xDiff = -
magRect.x;
magRect.x = 0;
}
if (magRect.y < 0) {
yDiff = -
magRect.y;
magRect.y = 0;
}
if ((magRect.x +
magRect.width) > imgWidth) {
wDiff =
(magRect.x + magRect.width) - imgWidth;
magRect.width
= imgWidth - magRect.x;
}
if ((magRect.y +
magRect.height) > imgHeight) {
hDiff =
(magRect.y + magRect.width) - imgHeight;
magRect.height
= imgHeight - magRect.y;
}
magnifiedImage =
(BufferedImage) scaledOriginalImage.getSubimage(magRect.x, magRect.y,
magRect.width, magRect.height);
Graphics2D magG =
magnifiedImage.createGraphics();
//qualityRequested = 1;
magG.setRenderingHint
(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
magG.setRenderingHint
(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//setRenderingHints
(magG, qualityRequested);
if (xDiff > 0 || yDiff
> 0 || wDiff > 0 || hDiff > 0) {
BufferedImage
newimg = new BufferedImage((int) (LENS_SIZE / zoomFactor), (int)
(LENS_SIZE / zoomFactor), BufferedImage.TYPE_INT_RGB);
Graphics newG
= newimg.createGraphics();
Graphics2D
gre2 = (Graphics2D)newG;
// qualityRequested
= 1;
gre2.setRenderingHint
(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
gre2.setRenderingHint
(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//setRenderingHints
(gre2, qualityRequested);
newG.drawImage
(magnifiedImage, (int) (xDiff), (int) (yDiff), null);
tmpimg =
newimg.getScaledInstance(LENS_SIZE, LENS_SIZE, Image.SCALE_DEFAULT);
newimg.flush();
} else {
tmpimg =
magnifiedImage.getScaledInstance(LENS_SIZE, LENS_SIZE,
Image.SCALE_DEFAULT);
}
if (magnifiedImage !=
null) magnifiedImage.flush();
} else {
repaint
();
if (displayableImage !=
null) {
Image img22 =
null;
img22 =
displayableImage.getScaledInstance(scaleWidth, scaleHeight,
Image.SCALE_DEFAULT);
scaledOriginalImage = new BufferedImage(scaleWidth,
scaleHeight, BufferedImage.TYPE_INT_RGB);
Graphics sg =
scaledOriginalImage.createGraphics();
Graphics2D
gre3 = (Graphics2D)sg;
gre3.setRenderingHint
(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
gre3.setRenderingHint
(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// qualityRequested
= 1;
//setRenderingHints
(gre3, qualityRequested);
sg.drawImage(img22, 0, 0, scaleWidth,
scaleHeight, null);
sg.dispose();
img22.flush();
img22 = null;
}
}
if (g == null &&
scaledOriginalImage != null) {
if
(hasFinishedPainting) {
repaint();
hasFinishedPainting = false;
}
} else if (magnifiedImage !=
null) {
float dx = (curDragX -
prevDragX);
float dy = (curDragY -
prevDragY);
float
x1,x2 ,y1,y2,minX,minY;
if (dx < 0) {
dx = -dx;
x1 = dx;
x2 = 0;
minX = (int)
curDragX;
} else {
x1 = 0;
x2 = dx;
minX = (int)
prevDragX;
}
if (dy < 0) {
dy = -dy;
y1 = dy;
y2 = 0;
minY = (int)
curDragY;
} else {
y1 = 0;
y2 = dy;
minY = (int)
prevDragY;
}
BufferedImage
mergedImage = new BufferedImage((int) (LENS_SIZE + dx), (int)
(LENS_SIZE + dy), BufferedImage.TYPE_INT_RGB);
BufferedImage
mergedDisplayImage = new BufferedImage((int) (LENS_SIZE + dx), (int)
(LENS_SIZE + dy), BufferedImage.TYPE_INT_RGB);
simpleBlur.filter
(mergedDisplayImage, mergedImage);
Graphics mg =
mergedImage.createGraphics();
Graphics2D gre4 =
(Graphics2D)mg;
//gre4.setComposite
(AlphaComposite.getInstance(AlphaComposite.SRC));
//gre4.setComposite
(AlphaComposite.getInstance(AlphaComposite.CLEAR));
//qualityRequested = 1;
//gre4.setRenderingHint
(RenderingHints.KEY_ALPHA_INTERPOLATION,
//
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
//gre4.setRenderingHint
(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
// gre4.setRenderingHint
(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
gre4.setRenderingHint
(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
gre4.setRenderingHint
(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//setRenderingHints
(gre4, qualityRequested);
int xx1 = (int) (minX -
imageX1 - LENS_SIZE / 2);
int yy1 = (int) (minY -
imageY1 - LENS_SIZE / 2);
int ww1 = (int)
(LENS_SIZE + dx);
int hh1 = (int)
(LENS_SIZE + dy);
int
xdiff1,ydiff1,wdiff1,hdiff1;
xdiff1 = xx1 < 0 ? -
xx1 : 0;
ydiff1 = yy1 < 0 ? -
yy1 : 0;
wdiff1 = ((xx1 + ww1) >
imgWidth) ? ((xx1 + ww1) - imgWidth) : 0;
hdiff1 = ((yy1 + hh1) >
imgHeight) ? ((yy1 + hh1) - imgHeight) : 0;
if (xdiff1 > 0) xx1 = 0;
if (ydiff1 > 0) yy1 = 0;
if (wdiff1 > 0) ww1
= imgWidth - xx1;
if (hdiff1 > 0) hh1 =
imgHeight - yy1;
mg.drawImage
(scaledOriginalImage.getSubimage(xx1, yy1, ww1, hh1), (int) (xdiff1),
(int) (ydiff1), null);
if (x2 == 0) dx = 0;
if (y2 == 0) dy = 0;
mg.setColor(Color.red);
mg.drawImage(tmpimg,
(int) (x2 + 0), (int) (y2 + 0), null);
paintStatus(g);
paintArrows(g);
//Graphics2D g2D =
mergedImage.createGraphics();
/**RenderingHints rh =
new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
rh.addRenderingHints
(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_N
EAREST_NEIGHBOR);*/
//g2D.setRenderingHints
(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_N
EAREST_NEIGHBOR);
g.drawImage
(mergedImage , (int) (curDragX - LENS_SIZE / 2 - dx), (int)
(curDragY - LENS_SIZE / 2 - dy), null);
mg.dispose();
mergedImage.flush();
mergedImage = null;
}
prevDragX = curDragX;
prevDragY = curDragY;
//}
}catch(Exception exp){
LogFile.write("ImagePanel" + exp.toString
());
}
}