Thumbimage
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class PictureProcessor {
/** The picture associated with this processor. */
private ImageIcon myPic;
/** The target image for the doFindTarget method. */
private ImageIcon targetPic;
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
public static final String IMAGE_JPEG = "jpeg";
public static final String IMAGE_JPG = "jpg";
public static final String IMAGE_PNG = "png";
/**
* Constructs a picture processor that manipulates a default image.
*/
public PictureProcessor() {
URL imgURL = getClass().getResource("image.jpg");
System.out.println("imgURL-->"+imgURL);
myPic = new ImageIcon(imgURL);
//myPic = new Picture(imgURL);
URL targetURL = getClass().getResource("/image.gif");
System.out.println("targetURL-->"+targetURL);
//targetPic = new Picture(targetURL);
getThumbnail(100, PictureProcessor.HORIZONTAL);
saveThumbnail(new File("thumb.jpg"), PictureProcessor.IMAGE_JPEG);
// The picUtil package seems to have a bug in
// handling the alpha channel. This is a kludge
// to fix things up... The picUtil package appears
// to take pixels that should be transparent and
// makes them pure white instead (with alpha==255,
// too). So, I'm going to take any pure white pixel
// in the target, and set the alpha to be 0.
//for (int i=0; i
//for (int j=0; j
//Pixel thePixel = targetPic.getPixel(j,i);
//
//PixelColour c = thePixel.getColour();
//
//if ((c.getRed()==255) && (c.getGreen()==255) &&
//
//(c.getBlue()==255)) c.setAlpha(0);
//
//thePixel.setColour(c);
//}
//}
}
public Image getThumbnail(int size, int dir)
{
if (dir == HORIZONTAL)
{
targetPic = new ImageIcon(
myPic.getImage().getScaledInstance(
size, -1, Image.SCALE_SMOOTH)
);
}
else
{
targetPic = new ImageIcon(
myPic.getImage().getScaledInstance(
-1, size, Image.SCALE_SMOOTH)
);
}
return targetPic.getImage();
}
//AND LATER THE PART THAT I AM HAVING TROUBLE WITH:
/**
* Computes a score of how well targetPic matches up
* with myPic, if the top-left corner of targetPic
* is positioned at row i and column j of myPic.
*
* Lower scores indicate a better match.
*
* The score is the weighted average (weighted by
* the alpha value in targetPic) of the distance
* between the two colours, squared. The squaring
* penalizes larger differences more heavily than
* simply adding up the differences.
*
* @param i The row to position the top-left corner
* of myPic; can be negative.
* @param j The column to position the top-let corner
* of myPic; can be negative.
* @return The score of how well the target matches at
* that position; lower is better.
*/
public void saveThumbnail(File file, String imageType)
{
if (targetPic != null)
{
BufferedImage bi = new BufferedImage(
targetPic.getIconWidth(),
targetPic.getIconHeight(),
BufferedImage.TYPE_INT_RGB
);
Graphics g = bi.getGraphics();
g.drawImage(targetPic.getImage(), 0, 0, null);
try
{
ImageIO.write(bi, imageType, file);
}
catch (IOException ioe)
{
System.out.println("Error occured saving thumbnail");
}
}
else
{
System.out.println("Thumbnail has not yet been created");
}
}
private double computeMatch(int i, int j) {
double score = 0.0;
int weightTotal = 0;
//for (int di=0; di
//for (int dj=0; dj
//if ((i+di>= 0) && (i+di < myPic.getImgHeight()) && (j+dj>= 0)
//
//&& (j+dj < myPic.getImgWidth())) {
//
//PixelColour imgColour = myPic.getPixel(j+dj, i+di).getColour();
//
//PixelColour targetColour = targetPic.getPixel(dj,di).getColour();
//int alpha = targetColour.getAlpha();
//
//
//
//int diff;
//
//int colourDistance=0;
//
//diff = imgColour.getRed()-targetColour.getRed();
//
//colourDistance += diff*diff;
//
//diff = imgColour.getGreen()-targetColour.getGreen();
//
//colourDistance += diff*diff;
//
//diff = imgColour.getBlue()-targetColour.getBlue();
//
//colourDistance += diff*diff;
//
//
//
//score += colourDistance*alpha;
//
//weightTotal += alpha;
//
//}
//
//}
///}
if (weightTotal==0) return 1.0/0.0; // Infinity
return score/weightTotal;
}
public static void main(String [] args)
{
PictureProcessor ct = new PictureProcessor();
}
}
-------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class CreateThumbnail
{
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
public static final String IMAGE_JPEG = "jpeg";
public static final String IMAGE_JPG = "jpg";
public static final String IMAGE_PNG = "png";
private ImageIcon image;
private ImageIcon thumb;
public CreateThumbnail(Image image)
{
System.out.println("111111111");
this.image = new ImageIcon(image);
}
public CreateThumbnail(String fileName)
{
System.out.println("2222222222222");
image = new ImageIcon(fileName);
}
public Image getThumbnail(int size, int dir)
{
if (dir == HORIZONTAL)
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(
size, -1, Image.SCALE_SMOOTH)
);
}
else
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(
-1, size, Image.SCALE_SMOOTH)
);
}
return thumb.getImage();
}
public Image getThumbnail(int size, int dir, int scale)
{
if (dir == HORIZONTAL)
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(size, -1, scale)
);
}
else
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(-1, size, scale)
);
}
return thumb.getImage();
}
public void saveThumbnail(File file, String imageType)
{
if (thumb != null)
{
BufferedImage bi = new BufferedImage(
thumb.getIconWidth(),
thumb.getIconHeight(),
BufferedImage.TYPE_INT_RGB
);
Graphics g = bi.getGraphics();
g.drawImage(thumb.getImage(), 0, 0, null);
try
{
ImageIO.write(bi, imageType, file);
}
catch (IOException ioe)
{
System.out.println("Error occured saving thumbnail");
}
}
else
{
System.out.println("Thumbnail has not yet been created");
}
}
public static void main(String [] args)
{
CreateThumbnail ct = new CreateThumbnail("image.jpg");
ct.getThumbnail(100, CreateThumbnail.HORIZONTAL);
ct.saveThumbnail(new File("thumb.jpg"), CreateThumbnail.IMAGE_JPEG);
}
}
Post a Comment