java 图像处理

  1 package com.linxi.page;
  2
  3 import java.awt.AlphaComposite;
  4 import java.awt.Color;
  5 import java.awt.Font;
  6 import java.awt.Graphics;
  7 import java.awt.Graphics2D;
  8 import java.awt.Image;
  9 import java.awt.Toolkit;
 10 import java.awt.color.ColorSpace;
 11 import java.awt.geom.AffineTransform;
 12 import java.awt.image.AffineTransformOp;
 13 import java.awt.image.BufferedImage;
 14 import java.awt.image.ColorConvertOp;
 15 import java.awt.image.CropImageFilter;
 16 import java.awt.image.FilteredImageSource;
 17 import java.awt.image.ImageFilter;
 18 import java.io.File;
 19 import java.io.IOException;
 20
 21
 22 import javax.imageio.ImageIO;
 23
 24
 25 /**
 26  * 图片处理工具类:<br>
 27  * 功能:缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
 28  * @author Administrator
 29  */
 30 public class ImageUtils {
 31
 32
 33     /**
 34      * 几种常见的图片格式
 35      */
 36     public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式
 37     public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组
 38     public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组
 39     public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式
 40     public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形
 41     public static String IMAGE_TYPE_PSD = "psd";// Photoshop的专用格式Photoshop
 42
 43
 44     /**
 45      * 程序入口:用于测试
 46      * @param args
 47      */
 48     public static void main(String[] args) {
 49         // 1-缩放图像:
 50         // 方法一:按比例缩放
 51         ImageUtils.scale("e:/abc.jpg", "e:/abc_scale.jpg", 2, true);//测试OK
 52         // 方法二:按高度和宽度缩放
 53         ImageUtils.scale2("e:/abc.jpg", "e:/abc_scale2.jpg", 500, 300, true);//测试OK
 54
 55
 56         // 2-切割图像:
 57         // 方法一:按指定起点坐标和宽高切割
 58         ImageUtils.cut("e:/abc.jpg", "e:/abc_cut.jpg", 0, 0, 400, 400 );//测试OK
 59         // 方法二:指定切片的行数和列数
 60         ImageUtils.cut2("e:/abc.jpg", "e:/", 2, 2 );//测试OK
 61         // 方法三:指定切片的宽度和高度
 62         ImageUtils.cut3("e:/abc.jpg", "e:/", 300, 300 );//测试OK
 63
 64
 65         // 3-图像类型转换:
 66         ImageUtils.convert("e:/abc.jpg", "GIF", "e:/abc_convert.gif");//测试OK
 67
 68
 69         // 4-彩色转黑白:
 70         ImageUtils.gray("e:/abc.jpg", "e:/abc_gray.jpg");//测试OK
 71
 72
 73         // 5-给图片添加文字水印:
 74         // 方法一:
 75         ImageUtils.pressText("我是水印文字","e:/abc.jpg","e:/abc_pressText.jpg","宋体",Font.BOLD,Color.white,80, 0, 0, 0.5f);//测试OK
 76         // 方法二:
 77         ImageUtils.pressText2("我也是水印文字", "e:/abc.jpg","e:/abc_pressText2.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.5f);//测试OK
 78
 79         // 6-给图片添加图片水印:
 80         ImageUtils.pressImage("e:/abc2.jpg", "e:/abc.jpg","e:/abc_pressImage.jpg", 0, 0, 0.5f);//测试OK
 81     }
 82
 83
 84     /**
 85      * 缩放图像(按比例缩放)
 86      * @param srcImageFile 源图像文件地址
 87      * @param result 缩放后的图像地址
 88      * @param scale 缩放比例
 89      * @param flag 缩放选择:true 放大; false 缩小;
 90      */
 91     public final static void scale(String srcImageFile, String result,
 92             int scale, boolean flag) {
 93         try {
 94             BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
 95             int width = src.getWidth(); // 得到源图宽
 96             int height = src.getHeight(); // 得到源图长
 97             if (flag) {// 放大
 98                 width = width * scale;
 99                 height = height * scale;
100             } else {// 缩小
101                 width = width / scale;
102                 height = height / scale;
103             }
104             Image image = src.getScaledInstance(width, height,
105                     Image.SCALE_DEFAULT);
106             BufferedImage tag = new BufferedImage(width, height,
107                     BufferedImage.TYPE_INT_RGB);
108             Graphics g = tag.getGraphics();
109             g.drawImage(image, 0, 0, null); // 绘制缩小后的图
110             g.dispose();
111             ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
112         } catch (IOException e) {
113             e.printStackTrace();
114         }
115     }
116
117
118     /**
119      * 缩放图像(按高度和宽度缩放)
120      * @param srcImageFile 源图像文件地址
121      * @param result 缩放后的图像地址
122      * @param height 缩放后的高度
123      * @param width 缩放后的宽度
124      * @param bb 比例不对时是否需要补白:true为补白; false为不补白;
125      */
126     public final static void scale2(String srcImageFile, String result, int height, int width, boolean bb) {
127         try {
128             double ratio = 0.0; // 缩放比例
129             File f = new File(srcImageFile);
130             BufferedImage bi = ImageIO.read(f);
131             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
132             // 计算比例
133             if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
134                 if (bi.getHeight() > bi.getWidth()) {
135                     ratio = (new Integer(height)).doubleValue()
136                             / bi.getHeight();
137                 } else {
138                     ratio = (new Integer(width)).doubleValue() / bi.getWidth();
139                 }
140                 AffineTransformOp op = new AffineTransformOp(AffineTransform
141                         .getScaleInstance(ratio, ratio), null);
142                 itemp = op.filter(bi, null);
143             }
144             if (bb) {//补白
145                 BufferedImage image = new BufferedImage(width, height,
146                         BufferedImage.TYPE_INT_RGB);
147                 Graphics2D g = image.createGraphics();
148                 g.setColor(Color.white);
149                 g.fillRect(0, 0, width, height);
150                 if (width == itemp.getWidth(null))
151                     g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
152                             itemp.getWidth(null), itemp.getHeight(null),
153                             Color.white, null);
154                 else
155                     g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
156                             itemp.getWidth(null), itemp.getHeight(null),
157                             Color.white, null);
158                 g.dispose();
159                 itemp = image;
160             }
161             ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
162         } catch (IOException e) {
163             e.printStackTrace();
164         }
165     }
166
167     /**
168      * 图像切割(按指定起点坐标和宽高切割)
169      * @param srcImageFile 源图像地址
170      * @param result 切片后的图像地址
171      * @param x 目标切片起点坐标X
172      * @param y 目标切片起点坐标Y
173      * @param width 目标切片宽度
174      * @param height 目标切片高度
175      */
176     public final static void cut(String srcImageFile, String result,
177             int x, int y, int width, int height) {
178         try {
179             // 读取源图像
180             BufferedImage bi = ImageIO.read(new File(srcImageFile));
181             int srcWidth = bi.getHeight(); // 源图宽度
182             int srcHeight = bi.getWidth(); // 源图高度
183             if (srcWidth > 0 && srcHeight > 0) {
184                 Image image = bi.getScaledInstance(srcWidth, srcHeight,
185                         Image.SCALE_DEFAULT);
186                 // 四个参数分别为图像起点坐标和宽高
187                 // 即: CropImageFilter(int x,int y,int width,int height)
188                 ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
189                 Image img = Toolkit.getDefaultToolkit().createImage(
190                         new FilteredImageSource(image.getSource(),
191                                 cropFilter));
192                 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
193                 Graphics g = tag.getGraphics();
194                 g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
195                 g.dispose();
196                 // 输出为文件
197                 ImageIO.write(tag, "JPEG", new File(result));
198             }
199         } catch (Exception e) {
200             e.printStackTrace();
201         }
202     }
203
204     /**
205      * 图像切割(指定切片的行数和列数)
206      * @param srcImageFile 源图像地址
207      * @param descDir 切片目标文件夹
208      * @param rows 目标切片行数。默认2,必须是范围 [1, 20] 之内
209      * @param cols 目标切片列数。默认2,必须是范围 [1, 20] 之内
210      */
211     public final static void cut2(String srcImageFile, String descDir,
212             int rows, int cols) {
213         try {
214             if(rows<=0||rows>20) rows = 2; // 切片行数
215             if(cols<=0||cols>20) cols = 2; // 切片列数
216             // 读取源图像
217             BufferedImage bi = ImageIO.read(new File(srcImageFile));
218             int srcWidth = bi.getHeight(); // 源图宽度
219             int srcHeight = bi.getWidth(); // 源图高度
220             if (srcWidth > 0 && srcHeight > 0) {
221                 Image img;
222                 ImageFilter cropFilter;
223                 Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
224                 int destWidth = srcWidth; // 每张切片的宽度
225                 int destHeight = srcHeight; // 每张切片的高度
226                 // 计算切片的宽度和高度
227                 if (srcWidth % cols == 0) {
228                     destWidth = srcWidth / cols;
229                 } else {
230                     destWidth = (int) Math.floor(srcWidth / cols) + 1;
231                 }
232                 if (srcHeight % rows == 0) {
233                     destHeight = srcHeight / rows;
234                 } else {
235                     destHeight = (int) Math.floor(srcWidth / rows) + 1;
236                 }
237                 // 循环建立切片
238                 // 改进的想法:是否可用多线程加快切割速度
239                 for (int i = 0; i < rows; i++) {
240                     for (int j = 0; j < cols; j++) {
241                         // 四个参数分别为图像起点坐标和宽高
242                         // 即: CropImageFilter(int x,int y,int width,int height)
243                         cropFilter = new CropImageFilter(j * destWidth, i * destHeight,
244                                 destWidth, destHeight);
245                         img = Toolkit.getDefaultToolkit().createImage(
246                                 new FilteredImageSource(image.getSource(),
247                                         cropFilter));
248                         BufferedImage tag = new BufferedImage(destWidth,
249                                 destHeight, BufferedImage.TYPE_INT_RGB);
250                         Graphics g = tag.getGraphics();
251                         g.drawImage(img, 0, 0, null); // 绘制缩小后的图
252                         g.dispose();
253                         // 输出为文件
254                         ImageIO.write(tag, "JPEG", new File(descDir
255                                 + "_r" + i + "_c" + j + ".jpg"));
256                     }
257                 }
258             }
259         } catch (Exception e) {
260             e.printStackTrace();
261         }
262     }
263
264
265     /**
266      * 图像切割(指定切片的宽度和高度)
267      * @param srcImageFile 源图像地址
268      * @param descDir 切片目标文件夹
269      * @param destWidth 目标切片宽度。默认200
270      * @param destHeight 目标切片高度。默认150
271      */
272     public final static void cut3(String srcImageFile, String descDir,
273             int destWidth, int destHeight) {
274         try {
275             if(destWidth<=0) destWidth = 200; // 切片宽度
276             if(destHeight<=0) destHeight = 150; // 切片高度
277             // 读取源图像
278             BufferedImage bi = ImageIO.read(new File(srcImageFile));
279             int srcWidth = bi.getHeight(); // 源图宽度
280             int srcHeight = bi.getWidth(); // 源图高度
281             if (srcWidth > destWidth && srcHeight > destHeight) {
282                 Image img;
283                 ImageFilter cropFilter;
284                 Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
285                 int cols = 0; // 切片横向数量
286                 int rows = 0; // 切片纵向数量
287                 // 计算切片的横向和纵向数量
288                 if (srcWidth % destWidth == 0) {
289                     cols = srcWidth / destWidth;
290                 } else {
291                     cols = (int) Math.floor(srcWidth / destWidth) + 1;
292                 }
293                 if (srcHeight % destHeight == 0) {
294                     rows = srcHeight / destHeight;
295                 } else {
296                     rows = (int) Math.floor(srcHeight / destHeight) + 1;
297                 }
298                 // 循环建立切片
299                 // 改进的想法:是否可用多线程加快切割速度
300                 for (int i = 0; i < rows; i++) {
301                     for (int j = 0; j < cols; j++) {
302                         // 四个参数分别为图像起点坐标和宽高
303                         // 即: CropImageFilter(int x,int y,int width,int height)
304                         cropFilter = new CropImageFilter(j * destWidth, i * destHeight,
305                                 destWidth, destHeight);
306                         img = Toolkit.getDefaultToolkit().createImage(
307                                 new FilteredImageSource(image.getSource(),
308                                         cropFilter));
309                         BufferedImage tag = new BufferedImage(destWidth,
310                                 destHeight, BufferedImage.TYPE_INT_RGB);
311                         Graphics g = tag.getGraphics();
312                         g.drawImage(img, 0, 0, null); // 绘制缩小后的图
313                         g.dispose();
314                         // 输出为文件
315                         ImageIO.write(tag, "JPEG", new File(descDir
316                                 + "_r" + i + "_c" + j + ".jpg"));
317                     }
318                 }
319             }
320         } catch (Exception e) {
321             e.printStackTrace();
322         }
323     }
324
325
326     /**
327      * 图像类型转换:GIF->JPG、GIF->PNG、PNG->JPG、PNG->GIF(X)、BMP->PNG
328      * @param srcImageFile 源图像地址
329      * @param formatName 包含格式非正式名称的 String:如JPG、JPEG、GIF等
330      * @param destImageFile 目标图像地址
331      */
332     public final static void convert(String srcImageFile, String formatName, String destImageFile) {
333         try {
334             File f = new File(srcImageFile);
335             f.canRead();
336             f.canWrite();
337             BufferedImage src = ImageIO.read(f);
338             ImageIO.write(src, formatName, new File(destImageFile));
339         } catch (Exception e) {
340             e.printStackTrace();
341         }
342     }
343
344
345     /**
346      * 彩色转为黑白
347      * @param srcImageFile 源图像地址
348      * @param destImageFile 目标图像地址
349      */
350     public final static void gray(String srcImageFile, String destImageFile) {
351         try {
352             BufferedImage src = ImageIO.read(new File(srcImageFile));
353             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
354             ColorConvertOp op = new ColorConvertOp(cs, null);
355             src = op.filter(src, null);
356             ImageIO.write(src, "JPEG", new File(destImageFile));
357         } catch (IOException e) {
358             e.printStackTrace();
359         }
360     }
361
362
363     /**
364      * 给图片添加文字水印
365      * @param pressText 水印文字
366      * @param srcImageFile 源图像地址
367      * @param destImageFile 目标图像地址
368      * @param fontName 水印的字体名称
369      * @param fontStyle 水印的字体样式
370      * @param color 水印的字体颜色
371      * @param fontSize 水印的字体大小
372      * @param x 修正值
373      * @param y 修正值
374      * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
375      */
376     public final static void pressText(String pressText,
377             String srcImageFile, String destImageFile, String fontName,
378             int fontStyle, Color color, int fontSize,int x,
379             int y, float alpha) {
380         try {
381             File img = new File(srcImageFile);
382             Image src = ImageIO.read(img);
383             int width = src.getWidth(null);
384             int height = src.getHeight(null);
385             BufferedImage image = new BufferedImage(width, height,
386                     BufferedImage.TYPE_INT_RGB);
387             Graphics2D g = image.createGraphics();
388             g.drawImage(src, 0, 0, width, height, null);
389             g.setColor(color);
390             g.setFont(new Font(fontName, fontStyle, fontSize));
391             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
392                     alpha));
393             // 在指定坐标绘制水印文字
394             g.drawString(pressText, (width - (getLength(pressText) * fontSize))
395                     / 2 + x, (height - fontSize) / 2 + y);
396             g.dispose();
397             ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile));// 输出到文件流
398         } catch (Exception e) {
399             e.printStackTrace();
400         }
401     }
402
403
404     /**
405      * 给图片添加文字水印
406      * @param pressText 水印文字
407      * @param srcImageFile 源图像地址
408      * @param destImageFile 目标图像地址
409      * @param fontName 字体名称
410      * @param fontStyle 字体样式
411      * @param color 字体颜色
412      * @param fontSize 字体大小
413      * @param x 修正值
414      * @param y 修正值
415      * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
416      */
417     public final static void pressText2(String pressText, String srcImageFile,String destImageFile,
418             String fontName, int fontStyle, Color color, int fontSize, int x,
419             int y, float alpha) {
420         try {
421             File img = new File(srcImageFile);
422             Image src = ImageIO.read(img);
423             int width = src.getWidth(null);
424             int height = src.getHeight(null);
425             BufferedImage image = new BufferedImage(width, height,
426                     BufferedImage.TYPE_INT_RGB);
427             Graphics2D g = image.createGraphics();
428             g.drawImage(src, 0, 0, width, height, null);
429             g.setColor(color);
430             g.setFont(new Font(fontName, fontStyle, fontSize));
431             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
432                     alpha));
433             // 在指定坐标绘制水印文字
434             g.drawString(pressText, (width - (getLength(pressText) * fontSize))
435                     / 2 + x, (height - fontSize) / 2 + y);
436             g.dispose();
437             ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile));
438         } catch (Exception e) {
439             e.printStackTrace();
440         }
441     }
442
443
444     /**
445      * 给图片添加图片水印
446      * @param pressImg 水印图片
447      * @param srcImageFile 源图像地址
448      * @param destImageFile 目标图像地址
449      * @param x 修正值。 默认在中间
450      * @param y 修正值。 默认在中间
451      * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
452      */
453     public final static void pressImage(String pressImg, String srcImageFile,String destImageFile,
454             int x, int y, float alpha) {
455         try {
456             File img = new File(srcImageFile);
457             Image src = ImageIO.read(img);
458             int wideth = src.getWidth(null);
459             int height = src.getHeight(null);
460             BufferedImage image = new BufferedImage(wideth, height,
461                     BufferedImage.TYPE_INT_RGB);
462             Graphics2D g = image.createGraphics();
463             g.drawImage(src, 0, 0, wideth, height, null);
464             // 水印文件
465             Image src_biao = ImageIO.read(new File(pressImg));
466             int wideth_biao = src_biao.getWidth(null);
467             int height_biao = src_biao.getHeight(null);
468             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
469                     alpha));
470             g.drawImage(src_biao, (wideth - wideth_biao) / 2,
471                     (height - height_biao) / 2, wideth_biao, height_biao, null);
472             // 水印文件结束
473             g.dispose();
474             ImageIO.write((BufferedImage) image,  "JPEG", new File(destImageFile));
475         } catch (Exception e) {
476             e.printStackTrace();
477         }
478     }
479
480
481     /**
482      * 计算text的长度(一个中文算两个字符)
483      * @param text
484      * @return
485      */
486     public final static int getLength(String text) {
487         int length = 0;
488         for (int i = 0; i < text.length(); i++) {
489             if (new String(text.charAt(i) + "").getBytes().length > 1) {
490                 length += 2;
491             } else {
492                 length += 1;
493             }
494         }
495         return length / 2;
496     }
497 }

http://blog.csdn.net/zhangzhikaixinya/article/details/8459400

时间: 2024-08-10 17:03:08

java 图像处理的相关文章

Java图像处理最快技术:ImageJ 学习第一篇

ImageJ是世界上最快的纯Java的图像处理程序.它可以过滤一个2048x2048的图像在0.1秒内(*).这是每秒40万像素!ImageJ的扩展通过使用内置的文本编辑器和Java编译器的ImageJ的开发插件.500多插件可用. 数据类型:8位灰度或索引色,16位无符号整数,32位浮点和RGB色彩. 文件格式:读写所有支持的数据类型为TIFF(非压缩)或原始数据.打开和保存GIF,JPEG,BMP,PNG,PGM,FITS和ASCII.打开DICOM.使用URL打开的TIFF.GIF文件.J

Java图像处理

要求实现简单的图像处理功能:     (1)打开一副图像:     (2)对打开的图像进行简单处理,至少包括两种操作,如均值滤波.直方图均衡化等:     (3)对处理之后的图像进行保存: import java.awt.EventQueue;import java.awt.event.*;import java.io.*;import javax.swing.*; /** * A program for viewing images. * @version 1.22 2017-3-17 * @

java 图像处理小软件(界面+图像处理)

一.界面学习 用java实现一个简易计算器(代码)如下: 1 /*CJSCalculator.java 2014.8.4 by cjs 2 *当点击含有加号的按钮时,则第一排第二个按钮的文本变为加号: 3 *当点击“OK”按钮时,将算出12+2的结果并在第一排最后一个按钮显示: 4 *减号,乘号,除号的功能类似.其中,数字可以自己输入,也可以固定不变. 5 *以上是简单的版本,如果有能力可以设计出更好更完善的计算器. 6 **/ 7 8 import java.awt.*; 9 import j

Java 图像处理框架-Marvin

网上看到,摘录过来的,暂时还没涉足这方面的东西 Marvin 1.4.5 的插件接口支持处理多个图像作为输入,新的插件可通过多个图片来确认背景,新的插件可使用多个图片来合并相同场景. Marvin 是一个Java开发的可扩展的图像处理框架,该框架主要提供以下几方面的功能: 基本图像操作; 从视频中捕获帧; 多线程的图像处理; 通过GUI界面集成插件; 插件性能分析; 通过插件进行功能扩展.

Atitit. 图像处理jpg图片的压缩 清理垃圾图片 java版本

Atitit. 图像处理jpg图片的压缩  清理垃圾图片 java版本 1. 清理图片压缩图片尺寸 1 2. 所以要使用ImageWriter 1 3. Thumbnails质量压缩builder.outputQuality(0.9); 2 4. attilax框架的处理 code 2 5. 到一篇文章提到如何控制jpg图片后压缩的质量 3 6. 参考 4 1. 清理图片压缩图片尺寸 目标::300kb>>>10kb.. 处理流程:::scale,outputQuality(0.5) 裁

atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本

atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本 1. 去除边框思路原理 1 2. Thumbnailator 是一个用来生成图像缩略图.裁切.旋转.添加水印等操作 2 3. OpenCL的Java库 JavaCL 2 4. Java Image Filters是一款基于Java的图像处理类库,特别是在图像滤镜特效方面, 2 4.1.1. 色彩调整 2 4.1.2. 变形和扭曲 5 5. JJIL 是一个Java 的图像处理类库,有超过60

百分之九十九的JAVA工作者都不知道的知识

1.Core Java部分 这是最基础的,对于一个java高级开发/设计人员,你需要对这一部分达到精通的水平,重点内容如下: a.面向对象编程思想(封装继承多态接口) b.字符串处理 c.java.lang包,java.util包等常用包 4.java异常处理 2.Java高级部分 a.Java I/O流 b.Java多线程技术 c.Java网络编程 d.Java Swing 后两项可以了解即可,如果项目需要可以深入研究 3.前端基本技能 * HTML + CSS网页开发 * JavaScrip

JAVA面试必备的知识宝典(一)

相关概念 面向对象的三个特征 封装,继承,多态.这个应该是人人皆知.有时候也会加上抽象. 多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消息就是函数调用).主要有以下优点: 可替换性:多态对已存在代码具有可替换性. 可扩充性:增加新的子类不影响已经存在的类结构. 接口性:多态是超累通过方法签名,想子类提供一个公共接口,由子类来完善或者重写它来实现的. 灵活性: 简化性: 代码中如何实现多态 实现多态主要有以下三种方式: 1. 接口实现

JAVA面试必备的知识宝典(二)

WeakReference与SoftReference的区别? 这点在四种引用类型中已经做了解释,这里简单说明一下即可: 虽然 WeakReference 与 SoftReference 都有利于提高 GC 和 内存的效率,但是 WeakReference ,一旦失去最后一个强引用,就会被 GC 回收,而软引用虽然不能阻止被回收,但是可以延迟到 JVM 内存不足的时候. 为什么要有不同的引用类型 不像C语言,我们可以控制内存的申请和释放,在Java中有时候我们需要适当的控制对象被回收的时机,因此