MAndViewer/src/Item.java

324 lines
9.9 KiB
Java
Raw Normal View History

2014-10-07 21:04:58 -04:00
import java.awt.Color;
2014-10-08 22:40:09 -04:00
import java.awt.Font;
import java.awt.FontMetrics;
2014-10-07 21:04:58 -04:00
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
2014-10-07 21:04:58 -04:00
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
2014-10-07 21:04:58 -04:00
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
2014-10-21 21:04:56 -04:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
2014-10-07 21:04:58 -04:00
import javax.imageio.ImageIO;
2014-10-21 21:04:56 -04:00
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
2014-10-07 21:04:58 -04:00
2014-10-10 14:11:17 -04:00
import MAndEngine.Engine;
2014-10-08 22:40:09 -04:00
2014-10-07 21:04:58 -04:00
public class Item {
// to track when to advance frame
2014-10-21 21:04:56 -04:00
private long lastTime = System.currentTimeMillis();
// every how many ms?
2014-10-21 21:04:56 -04:00
private long interval = 100;
// because we need something to tell us which image we're looking at!
2014-10-21 21:04:56 -04:00
private int pointer = 0;
2014-10-10 14:11:17 -04:00
// cropped to a thumb nail, of size 80*80
2014-10-07 21:04:58 -04:00
private final BufferedImage thumbnail;
2014-10-10 14:11:17 -04:00
// the original image, not cropped or anything.
private final BufferedImage[] image;
// Absolute path, i think.
2014-10-07 21:04:58 -04:00
private final String path;
private String name;
// we need this because im scared.
// if literally anything in the creating of an item
// goes MODERATELY wrong, you set this to FALSE.
// then we don't try and render something that
// makes no sense.
// set this to false by the end of the constructor
2014-10-08 22:40:09 -04:00
// and when it comes back to be added to the list,
// it'll get thrown out.
2014-10-07 21:04:58 -04:00
private boolean seemsLegit = true;
public Item(String path) {
BufferedImage thumbnail = null;
2014-10-10 14:11:17 -04:00
BufferedImage[] images = null;
2014-10-07 21:04:58 -04:00
try {
File file = new File(path);
if (file.isDirectory()) {
// thumbnail =
// ImageCreator.creatImageWithStripes(Viewer.THUMBNAIL_SIZE,
// Viewer.THUMBNAIL_SIZE, Color.BLUE);
try {
//just... like its not hard but its not properly spaced out to be readable
//so just trust drunk on life marcus that it tooootally works.
//doesn't mess with image yet though so feel free to implement that.
//yeah, TODO...
Font font = new Font("Serif", Font.PLAIN, 50);
thumbnail = new BufferedImage(Viewer.THUMBNAIL_SIZE, Viewer.THUMBNAIL_SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) thumbnail.getGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(font);
FontMetrics metrics = g.getFontMetrics();
String string = "" + file.getName();
if(string.length() > 8) {
string = string.substring(0, 9) + "...";
}
int padding = 5;
int frameSize = Viewer.THUMBNAIL_SIZE - 2 * padding;
Rectangle2D bounds = metrics.getStringBounds(string, g);
BufferedImage text = new BufferedImage((int) bounds.getWidth() + 2, (int) bounds.getHeight() + 2 + metrics.getMaxDescent(), BufferedImage.TYPE_INT_ARGB);
Graphics2D textGraphics = (Graphics2D) text.getGraphics();
textGraphics.setFont(font);
textGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
textGraphics.setColor(Color.BLACK);
textGraphics.drawString(string, 1, (int)bounds.getHeight());
text = fitImageScale(text, frameSize, frameSize);
g.setColor(new Color(220, 220, 220));
g.fillRect(0, 0, Viewer.THUMBNAIL_SIZE, Viewer.THUMBNAIL_SIZE);
g.setColor(new Color(170, 170, 170));
g.drawRect(0, 0, Viewer.THUMBNAIL_SIZE - 1, Viewer.THUMBNAIL_SIZE - 1);
g.drawImage(text, padding, padding, null);
//now try to make a mini album thing showing us what we're missing!
//queue collage mode
} catch (Exception e) {
e.printStackTrace();
}
2014-10-07 21:04:58 -04:00
name = file.getName();
2014-10-10 14:11:17 -04:00
} else {
if (!path.endsWith(".gif")) {
// try and do the image thing!
images = new BufferedImage[] { ImageIO.read(file) };
} else {
// do da gif ting! O YA, I CAN DO DAT NAO
2014-10-21 21:04:56 -04:00
ArrayList<BufferedImage> imageList = getGif(path);
images = new BufferedImage[imageList.size()];
for (int i = 0; i < imageList.size(); i++)
2014-10-21 21:04:56 -04:00
images[i] = imageList.get(i);
2014-10-10 14:11:17 -04:00
}
thumbnail = (fillImageScale(images[0], 80, 80));
2014-10-07 21:04:58 -04:00
path = file.getAbsolutePath();
name = "";
}
} catch (Exception e) {
seemsLegit = false;
2014-10-08 22:40:09 -04:00
System.out.println("wat: " + e.getMessage());
System.out.println("wat was in " + path);
2014-10-07 21:04:58 -04:00
}
2014-10-10 14:11:17 -04:00
this.image = images;
2014-10-07 21:04:58 -04:00
this.path = path;
this.thumbnail = thumbnail;
}
2014-10-10 14:11:17 -04:00
2014-10-07 21:04:58 -04:00
public boolean getSeemsLegit() {
return seemsLegit;
}
private static BufferedImage fillImageScale(BufferedImage image, int width, int height) throws IOException {
2014-10-10 14:11:17 -04:00
2014-10-07 21:04:58 -04:00
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
2014-10-10 14:11:17 -04:00
2014-10-07 21:04:58 -04:00
double scaleY = (double) height / imageHeight;
2014-10-10 14:11:17 -04:00
double scaleX = (double) width / imageWidth;
// fill or fit bit
if (scaleX < scaleY)
scaleX = scaleY;
else
scaleY = scaleX;
// give us the transform object thing
2014-10-08 22:40:09 -04:00
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
2014-10-10 14:11:17 -04:00
// then make the scaling algorithm thing.
2014-10-07 21:04:58 -04:00
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
2014-10-10 14:11:17 -04:00
// out new image that we need to crop onto the buffer with the right
// dimensions.
BufferedImage newImage = bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scaleX), (int) (imageHeight * scaleY), image.getType()));
// Image newImage = image.getScaledInstance((int) (imageWidth * scaleX),
// (int) (imageWidth * scaleY), Image.SCALE_SMOOTH);
// make the buffer
2014-10-08 22:40:09 -04:00
BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = buffer.getGraphics();
2014-10-10 14:11:17 -04:00
int newImageWidth = newImage.getWidth(null);
int newImageHeight = newImage.getHeight(null);
Engine.log("original: " + imageWidth + " x " + imageHeight);
Engine.log("new: " + width + " x " + height);
Engine.log("after: " + newImageWidth + " x " + newImageHeight);
// do math, shove it on.
g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null);
// return dat
2014-10-08 22:40:09 -04:00
return buffer;
2014-10-07 21:04:58 -04:00
}
2014-10-10 14:11:17 -04:00
private static BufferedImage fitImageScale(BufferedImage image, int width, int height) throws IOException {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
double scaleY = (double) height / imageHeight;
double scaleX = (double) width / imageWidth;
// fill or fit bit
//heh, hutch
if (scaleX > scaleY)
scaleX = scaleY;
else
scaleY = scaleX;
// give us the transform object thing
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
// then make the scaling algorithm thing.
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
// out new image that we need to crop onto the buffer with the right
// dimensions.
BufferedImage newImage = bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scaleX), (int) (imageHeight * scaleY), image.getType()));
// Image newImage = image.getScaledInstance((int) (imageWidth * scaleX),
// (int) (imageWidth * scaleY), Image.SCALE_SMOOTH);
// make the buffer
BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = buffer.getGraphics();
int newImageWidth = newImage.getWidth(null);
int newImageHeight = newImage.getHeight(null);
Engine.log("original: " + imageWidth + " x " + imageHeight);
Engine.log("new: " + width + " x " + height);
Engine.log("after: " + newImageWidth + " x " + newImageHeight);
// do math, shove it on.
g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null);
// return dat
return buffer;
}
2014-10-07 21:04:58 -04:00
public BufferedImage getImage() {
2014-10-21 21:04:56 -04:00
if (System.currentTimeMillis() > lastTime + interval) {
pointer++;
2014-10-21 21:04:56 -04:00
if (pointer == image.length) {
pointer = 0;
}
lastTime = System.currentTimeMillis();
}
System.out.println(pointer);
2014-10-21 21:04:56 -04:00
return image[pointer];
2014-10-07 21:04:58 -04:00
}
public BufferedImage getThumbnail() {
return thumbnail;
}
public String getPath() {
return path;
}
public String getName() {
return name;
}
2014-10-21 21:04:56 -04:00
public static ArrayList<BufferedImage> getGif(String path) {
try {
ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
String[] imageatt = new String[] { "imageLeftPosition", "imageTopPosition", "imageWidth", "imageHeight" };
ImageReader reader = (ImageReader) ImageIO.getImageReadersByFormatName("gif").next();
ImageInputStream ciis = ImageIO.createImageInputStream(new File(path));
reader.setInput(ciis, false);
int noi = reader.getNumImages(true);
BufferedImage master = null;
for (int i = 0; i < noi; i++) {
BufferedImage image = reader.read(i);
IIOMetadata metadata = reader.getImageMetadata(i);
Node tree = metadata.getAsTree("javax_imageio_gif_image_1.0");
NodeList children = tree.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node nodeItem = children.item(j);
if (nodeItem.getNodeName().equals("ImageDescriptor")) {
Map<String, Integer> imageAttr = new HashMap<String, Integer>();
for (int k = 0; k < imageatt.length; k++) {
NamedNodeMap attr = nodeItem.getAttributes();
Node attnode = attr.getNamedItem(imageatt[k]);
imageAttr.put(imageatt[k], Integer.valueOf(attnode.getNodeValue()));
}
// if first time round
if (i == 0)
2014-10-21 21:04:56 -04:00
master = new BufferedImage(imageAttr.get("imageWidth"), imageAttr.get("imageHeight"), BufferedImage.TYPE_INT_RGB);
master.getGraphics().drawImage(image, imageAttr.get("imageLeftPosition"), imageAttr.get("imageTopPosition"), null);
}
}
BufferedImage newThing = new BufferedImage(master.getWidth(), master.getHeight(), master.getType());
newThing.getGraphics().drawImage(master, 0, 0, null);
images.add(newThing);
}
2014-10-21 21:04:56 -04:00
return images;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
2014-10-07 21:04:58 -04:00
}