关于图片的一些处理 博客分类: JQueryJ2SE 图片合成裁剪base64水印
程序员文章站
2024-03-25 13:17:40
...
一些图片处理:
//两张图片合成 public static void composePic(String filesrc,String logosrc,String outsrc,int x,int y) throws Exception { try { File bgfile = new File(filesrc); Image bg_src = javax.imageio.ImageIO.read(bgfile); File logofile = new File(logosrc); Image logo_src = javax.imageio.ImageIO.read(logofile); int bg_width = bg_src.getWidth(null); int bg_height = bg_src.getHeight(null); int logo_width = logo_src.getWidth(null);; int logo_height = logo_src.getHeight(null); BufferedImage tag = new BufferedImage(bg_width, bg_height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = tag.createGraphics(); g2d.drawImage(bg_src, 0, 0, bg_width, bg_height, null); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,1.0f)); //透明度设置开始 g2d.drawImage(logo_src,x,y,logo_width,logo_height, null); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); //透明度设置 结束 FileOutputStream out = new FileOutputStream(outsrc); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); }catch (Exception e) { e.printStackTrace(); log.info("图片合成失败:" + e.getMessage()); throw new Exception(e); } } //生成指定大小的图片 public static void createImg(String path, int width, int height) throws Exception{ File file = new File(path); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D)bi.getGraphics(); g2.setBackground(Color.WHITE); g2.clearRect(0, 0, width, height); g2.setPaint(Color.RED); ImageIO.write(bi, "jpg", file); }
//图片base64编码与解码
public class ImgConvert { public static String getBase64(String path) throws Exception {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理 InputStream in = null; byte[] data = null; if(null == path || "".equals(path)) return ""; //读取图片字节数组 File file = new File(path); in = new FileInputStream(file); data = new byte[in.available()]; in.read(data); in.close(); //对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);//返回Base64编码过的字节数组字符串 } public static boolean getImage(String imgStr, String path) {//对字节数组字符串进行Base64解码并生成图片 if (imgStr == null) //图像数据为空 return false; BASE64Decoder decoder = new BASE64Decoder(); try { //Base64解码 byte[] b = decoder.decodeBuffer(imgStr); for(int i=0;i<b.length;++i) { if(b[i]<0) {//调整异常数据 b[i]+=256; } } //生成jpeg图片 OutputStream out = new FileOutputStream(path); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }
//图片上添加文字 public class ImgMessage { public static void main(String[] args) { String url = "D:\\Blue.jpg"; } public String writeMessage(String message, String url, int offsetTop, int offsetLeft) { try { // 读取模板图片内容 JPEGImageDecoder decoder = JPEGCodec .createJPEGDecoder(new FileInputStream(url)); // 读取图片路径 BufferedImage image = decoder.decodeAsBufferedImage(); Graphics2D g = image.createGraphics();// 得到图形上下文 g.setColor(Color.BLACK); // 设置画笔颜色 RenderingHints rh = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); rh.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHints(rh); g.setColor(Color.black); Font font = new Font("宋体", Font.BOLD, 12); g.setFont(font); FontMetrics fm = g.getFontMetrics(font); // 设置换行操作 int fontHeight = fm.getHeight();// 字符的高度 int rowIndex = 1; int left = offsetLeft; for (int i = 0; i < message.length(); i++) { char c = message.charAt(i); int charWidth = fm.charWidth(c); // 字符的宽度 // 另起一行 if (Character.isISOControl(c)) { rowIndex++; left = offsetLeft; } g.drawString(String.valueOf(c), left, offsetTop + rowIndex * fontHeight); // 把一个个写到图片上 left += charWidth; // 设置下字符的间距 } g.dispose(); String outFileName = url; // 图片要存储的路径 FileOutputStream out = new FileOutputStream(outFileName);// 通过流操作把文字图片结合 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); return outFileName; } catch (Exception e) { e.printStackTrace(); return null; } } }
//图片裁剪
/** * 按前台截取的大小对原图进行截取 * * @param img * 原图文件 * @param dest * 保存地址 * @param top * 左上角点的Y * @param left * 左上角点的X * @param width * 图片的宽度 * @param height * 图片的高度 * @param type * 图片类型 * @return * @throws IOException */ public static void resizeImage(File img, String dest, int top, int left, int width, int height, String type) throws Exception { try{ File fileDest = new File(dest); BufferedImage bi = (BufferedImage) ImageIO.read(img); BufferedImage bi_cropper = bi.getSubimage(left, top, width, height); ImageIO.write(bi_cropper, type, fileDest); }catch(Exception e){ e.printStackTrace(); log.error("图片裁剪失败:" + e.getMessage()); throw new Exception(e.getMessage()); } } /** * 对gif图形进行裁剪 * @param img * @param dest * @param top * @param left * @param width * @param height */ public void resizeGifImage(File img, String dest, int top, int left, int width, int height){ File des = new File(dest); GifImage gif; try { gif = GifDecoder.decode(img); Rectangle rect = new Rectangle(top, left, width, height-3); GifImage cropIMG = GifTransformer.crop(gif, rect); GifEncoder.encode(cropIMG, des); } catch (IOException e) { e.printStackTrace(); } }
jquery插件:
easydrag: 一个简单的拖放插件,可用于获取拖放div的id再获取位置(top,left),有需要可以通过iframe进行限制以获取准确的top,left。 jquery.form: 以ajax方式提交form表单。 jcarousel: 可横向和纵向的滚动显示图片。jcrop: 裁剪插件。 kindeditor:一个在线编辑插件,部分功能可单独使用,如取色器。
java以积竖线分隔split("\\|");
js的replace替换所有逗号replace(/,/g, '_')应用正则,得去学习学习正则。