1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
| package com.lrs.util;
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; import java.awt.image.ImageFilter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;
import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;
@SuppressWarnings("restriction") public class ImageUtils {
public final static void pressImage(String pressImg, String targetImg, float drection, float alpha) { try { float Alpha = alpha; File _file = new File(targetImg); Image src = ImageIO.read(_file); int wideth = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(src, 0, 0, wideth, height, null); File _filebiao = new File(pressImg); Image src_biao = ImageIO.read(_filebiao); int wideth_biao = src_biao.getWidth(null); int height_biao = src_biao.getHeight(null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Alpha)); if (drection < 0.5) { g.drawImage(src_biao, 50, 50, wideth_biao, height_biao, null); } else if (drection == 0.5) { g.drawImage(src_biao, wideth / 2, height / 2, null); } else { g.drawImage(src_biao, wideth - wideth_biao - 50, height - height_biao - 50, null); } g.dispose(); FileOutputStream out = new FileOutputStream(targetImg); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } catch (Exception e) { e.printStackTrace(); } }
public final static void pressImage(String pressImg, String targetImg, String newPath, float drection, float alpha) { try { OutputStream os = null; float Alpha = alpha; File _file = new File(targetImg); Image src = ImageIO.read(_file); int wideth = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(src, 0, 0, wideth, height, null); File _filebiao = new File(pressImg); Image src_biao = ImageIO.read(_filebiao); int wideth_biao = src_biao.getWidth(null); int height_biao = src_biao.getHeight(null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Alpha)); if (drection < 0.5) { g.drawImage(src_biao, 50, 50, wideth_biao, height_biao, null); } else if (drection == 0.5) { g.drawImage(src_biao, wideth / 2, height / 2, null); } else { g.drawImage(src_biao, wideth - wideth_biao - 50, height - height_biao - 50, null); } g.dispose(); os = new FileOutputStream(newPath); ImageIO.write(image, "JPG", os); os.close(); } catch (Exception e) { e.printStackTrace(); } }
public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, float drection, float alpha) { try { System.out.println(pressText.length()); float Alpha = alpha; File _file = new File(targetImg); Image src = ImageIO.read(_file); int wideth = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null); g.setColor(color); g.setFont(new Font(fontName, fontStyle, fontSize)); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Alpha)); if (drection < 0.5) { g.drawString(pressText, fontSize, fontSize); } else if (drection == 0.5) { g.drawString(pressText, wideth / 2, height / 2); } else { g.drawString(pressText, wideth - (getTextLength(pressText) * fontSize) - fontSize, height - fontSize); } g.dispose(); FileOutputStream out = new FileOutputStream(targetImg); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } catch (Exception e) { System.out.println(e); } }
public static void rotateImage(String filePath, String newPath, String logoPath, int degree, String alpha) { OutputStream os = null; try { if (!"".equals(filePath)) { Image srcImg = ImageIO.read(new File(filePath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
g.rotate(Math.toRadians(degree), 0, 0);
ImageIcon imgIcon = new ImageIcon(logoPath);
Image img = imgIcon.getImage();
int width = srcImg.getWidth(null); int height = srcImg.getHeight(null);
int width1 = img.getWidth(null); int height1 = img.getHeight(null);
if (alpha == null || alpha.equals("")) { alpha = "1"; } g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Float.parseFloat(alpha)));
int x = -width / 2; int y = -height / 2; while (x < width * 2) { y = -height / 2; while (y < height * 1.5) { g.drawImage(img, x, y, null); y += height1 + 300; } x += width1 + 300; }
g.dispose();
os = new FileOutputStream(newPath);
ImageIO.write(buffImg, "JPG", os); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) { os.close(); } } catch (Exception e) { e.printStackTrace(); } } }
public static int getTextLength(String text) { int length = text.length(); for (int i = 0; i < text.length(); i++) { String s = String.valueOf(text.charAt(i)); if (s.getBytes().length > 1) { length++; } } length = length % 2 == 0 ? length / 2 : length / 2 + 1; return length; }
public static List<String> getImageSrc(String htmlCode) { List<String> imageSrcList = new ArrayList<String>(); Pattern p = Pattern.compile( "<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg|\\.bmp|\\.eps|\\.gif|\\.mif|\\.miff|\\.png|\\.tif|\\.tiff|\\.svg|\\.wmf|\\.jpe|\\.jpeg|\\.dib|\\.ico|\\.tga|\\.cut|\\.pic)\\b)[^>]*>", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(htmlCode); String quote = null; String src = null; while (m.find()) { quote = m.group(1); src = (quote == null || quote.trim().length() == 0) ? m.group(2).split("\\s+")[0] : m.group(2); imageSrcList.add(src); } return imageSrcList; }
public final static void cut(String srcImageFile, String result, int x, int y, int width, int height) { try { BufferedImage bi = ImageIO.read(new File(srcImageFile)); 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, "png", new File(result)); } } catch (Exception e) { e.printStackTrace(); } }
public final static void cut(InputStream in, String result, int x, int y, int width, int height) { try { BufferedImage bi = ImageIO.read(in); 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, "png", new File(result)); } } catch (Exception e) { e.printStackTrace(); } }
public static void cut(int x1, int y1, int width, int height, String sourcePath, String descpath) { FileInputStream is = null; ImageInputStream iis = null; try { is = new FileInputStream(sourcePath); String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") + 1); Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(fileSuffix); ImageReader reader = it.next(); iis = ImageIO.createImageInputStream(is); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect = new Rectangle(x1, y1, width, height); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, fileSuffix, new File(descpath)); } catch (Exception ex) { ex.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is = null; } if (iis != null) { try { iis.close(); } catch (IOException e) { e.printStackTrace(); } iis = null; } } }
public static void crop(InputStream input, OutputStream output, int x, int y, int w, int h, boolean isPNG) throws Exception { try { BufferedImage srcImg = ImageIO.read(input); int tmpWidth = srcImg.getWidth(); int tmpHeight = srcImg.getHeight(); int xx = Math.min(tmpWidth - 1, x); int yy = Math.min(tmpHeight - 1, y);
int ww = w; if (xx + w > tmpWidth) { ww = Math.max(1, tmpWidth - xx); } int hh = h; if (yy + h > tmpHeight) { hh = Math.max(1, tmpHeight - yy); }
BufferedImage dest = srcImg.getSubimage(xx, yy, ww, hh);
BufferedImage tag = new BufferedImage(w, h, isPNG ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(dest, 0, 0, null); ImageIO.write(tag, isPNG ? "png" : "jpg", output); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } finally { if (input != null) { input.close(); } if (output != null) { output.close(); } } }
public static void crop(InputStream input, String result, int x, int y, int w, int h, boolean isPNG) throws Exception { try { BufferedImage srcImg = ImageIO.read(input); int tmpWidth = srcImg.getWidth(); int tmpHeight = srcImg.getHeight(); int xx = Math.min(tmpWidth - 1, x); int yy = Math.min(tmpHeight - 1, y);
int ww = w; if (xx + w > tmpWidth) { ww = Math.max(1, tmpWidth - xx); } int hh = h; if (yy + h > tmpHeight) { hh = Math.max(1, tmpHeight - yy); }
BufferedImage dest = srcImg.getSubimage(xx, yy, ww, hh);
BufferedImage tag = new BufferedImage(w, h, isPNG ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(dest, 0, 0, null); ImageIO.write(tag, isPNG ? "png" : "jpg", new FileOutputStream(new File(result))); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } finally { if (input != null) { input.close(); } } }
public static void main(String[] args) throws FileNotFoundException, Exception { System.out.print("添加成功"); crop(new FileInputStream("E:\\lrs_img\\timg1.jpg"), "E:\\lrs_img\\timg121.png", 148, 14, 251, 251, true); } }
|