/** * 图像切割(指定切片的宽度和高度) * @param bi 原图像 * @param x 裁剪原图像起点坐标X * @param y 裁剪原图像起点坐标Y * @param width 目标切片宽度 * @param height 目标切片高度 * @return */ public static BufferedImage cut(BufferedImage bi,int x, int y, int width, int height) { BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = tag.createGraphics(); tag = g2.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); g2.dispose(); g2 = tag.createGraphics(); int srcWidth = bi.getHeight(); // 源图宽度 int srcHeight = bi.getWidth(); // 源图高度 if (srcWidth > 0 && srcHeight > 0) { ImageFilter cropFilter = new CropImageFilter(x, y, width, height); Image img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(bi.getSource(),cropFilter)); g2.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图 g2.dispose(); } return tag; }
/** * 图像切割(按指定起点坐标和宽高切割) * @param srcImg 源图像地址 * @param outImg 切片后的图像地址 * @param x 目标切片起点坐标X * @param y 目标切片起点坐标Y * @param width 目标切片宽度 * @param height 目标切片高度 * @param imageType 图片类型 */ public static void cut(File srcImg, File outImg, int x, int y, int width, int height,String imageType) throws Exception{ //读取源图像 BufferedImage bi = ImageIO.read(srcImg); int srcWidth = bi.getHeight();//源图宽度 int srcHeight = bi.getWidth();//源图高度 if (srcWidth > 0 && srcHeight > 0) { Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT); //四个参数分别为图像起点坐标和宽高 //即:CropImageFilter(int x,int y,int width,int height) ImageFilter cropFilter = new CropImageFilter(x, y, width,height); Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, width, height, null);//绘制切割后的图 g.dispose(); //输出为文件 ImageIO.write(tag, imageType, outImg); } }
/** * Image clip. * * @param g * the g * @param ci * the ci */ private void imageClip(Graphics2D g, CanvasInfo ci) { URL u; try { u = new URL(ci.getImage().getSrc()); URLConnection con = u.openConnection(); con.setRequestProperty("User-Agent", UserAgentContext.DEFAULT_USER_AGENT); Image img = ImageIO.read(con.getInputStream()); img = createImage(new FilteredImageSource(img.getSource(), new CropImageFilter(ci.getSx(), ci.getSy(), ci.getSw(), ci.getSh()))); g.clip(new Rectangle2D.Float(ci.getDx(), ci.getDy(), ci.getDw(), ci.getDh())); g.drawImage(img, ci.getDx(), ci.getDy(), ci.getDw(), ci.getDh(), this); } catch (Exception e) { logger.error(e.getLocalizedMessage()); } }
public Jimage cut(int x, int y, int width, int height) { try { BufferedImage bi = this.imageis; int srcWidth = bi.getHeight(); int srcHeight = bi.getWidth(); if (srcWidth > 0 && srcHeight > 0) { Image imagex = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); ImageFilter cropFilter = new CropImageFilter(x, y, width, height); Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(imagex.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图 g.dispose(); this.imageis = tag; } } catch (Exception e) { e.printStackTrace(); } return this; }
/** * 图像切割(按指定起点坐标和宽高切割) * @param srcImageFile 源图像地址 * @param result 切片后的图像地址 * @param x 目标切片起点坐标X * @param y 目标切片起点坐标Y * @param width 目标切片宽度 * @param height 目标切片高度 */ public final static void cut(File src, File tar, int x, int y, int width, int height) { try { // 读取源图像 BufferedImage bi = ImageIO.read(src); int srcWidth = bi.getHeight(); // 源图宽度 int srcHeight = bi.getWidth(); // 源图高度 if (srcWidth > 0 && srcHeight > 0) { Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); // 四个参数分别为图像起点坐标和宽高 // 即: CropImageFilter(int x,int y,int width,int height) ImageFilter cropFilter = new CropImageFilter(x, y, width, height); Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图 g.dispose(); // 输出为文件 File dir = tar.getParentFile(); if(!dir.exists()){ dir.mkdirs(); } ImageIO.write(tag, "JPEG", tar); } } catch (Exception e) { e.printStackTrace(); } }
/** * 图像切割(按指定起点坐标和宽高切割) * * @param is * 源图像输入流 * @param os * 切割后输出流 * @param x * 目标切片起点坐标X * @param y * 目标切片起点坐标Y * @param width * 目标切片宽度 * @param height * 目标切片高度 */ public final static void cut(InputStream is, OutputStream os, int x, int y, int width, int height) { try { // 读取源图像 BufferedImage bi = ImageIO.read(is); int srcWidth = bi.getHeight(); // 源图宽度 int srcHeight = bi.getWidth(); // 源图高度 if (srcWidth > 0 && srcHeight > 0) { Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); // 四个参数分别为图像起点坐标和宽高 ImageFilter cropFilter = new CropImageFilter(x, y, width, height); Image img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图 g.dispose(); // 输出为文件 ImageIO.write(tag, IMAGE_TYPE_JPEG, os); } } catch (Exception e) { e.printStackTrace(); } }
public static void main (String[] args) { new CropImageFilter(20, 20, 40, 50).clone(); }
public void define() { room = new Room(); save = new Save(); store = new Store(); for(int i = 0; i < tileset_ground.length; i++) { tileset_ground[i] = new ImageIcon("res" + File.separator + "tileset_ground.png").getImage(); if(Frame.BigRes) { tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, room.blockSize*i/2, room.blockSize/2, room.blockSize/2))); } else { tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, room.blockSize*i, room.blockSize, room.blockSize))); } } for(int i = 0; i < tileset_air.length; i++) { tileset_air[i] = new ImageIcon("res" + File.separator + "tileset_air.png").getImage(); if(Frame.BigRes) { tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, room.blockSize*i/2, room.blockSize/2, room.blockSize/2))); } else { tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, room.blockSize*i, room.blockSize, room.blockSize))); } } tileset_res[0] = new ImageIcon("res" + File.separator + "cell.png").getImage(); tileset_res[1] = new ImageIcon("res" + File.separator + "heart.png").getImage(); tileset_res[2] = new ImageIcon("res" + File.separator + "coin.png").getImage(); tileset_res[3] = new ImageIcon("res" + File.separator + "level.png").getImage(); tileset_mob[0] = new ImageIcon("res" + File.separator + "mob.png").getImage(); firstSpawn = true; spawnFrame = 0; health = 100; try { if(saveFile.exists()) { saveFileExists = true; } else { saveFileExists = false; File tmpCreateFolder = new File("save"); tmpCreateFolder.mkdirs(); saveFile.createNewFile(); } if(newLaunch) { newLaunch = false; if(saveFileExists) { Scanner loadScanner = new Scanner(saveFile); while(loadScanner.hasNext()) { startingLevel = loadScanner.nextInt(); } loadScanner.close(); } } File loadLevelFile = new File("levels" + File.separator + "mission" + startingLevel + ".btd"); if(loadLevelFile.exists()) { save.loadSave(loadLevelFile); } else { isGameEnding = true; loadLevelFile = new File("levels" + File.separator + "mission" + 1 + ".btd"); save.loadSave(loadLevelFile); } } catch (Exception e) {} Store.towerCounter = 0; for(int i = 0; i < mobs.length; i++) { mobs[i] = new Mob(); } }