非常有用的20个Java程序片段
来源:优易学  2011-12-16 16:47:42   【优易学:中国教育考试门户网】   资料下载   IT书店

 

  // draw original image to thumbnail image object and

  // scale it to the new size on-the-fly

  BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);

  Graphics2D graphics2D = thumbImage.createGraphics();

  graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

  graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

  // save thumbnail image to outFilename

  BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));

  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

  JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

  quality = Math.max(0, Math.min(quality, 100));

  param.setQuality((float)quality / 100.0f, false);

  encoder.setJPEGEncodeParam(param);

  encoder.encode(thumbImage);

  out.close();

  }

  9. 创建 JSON 格式的数据

  Java代码

  请先阅读这篇文章 了解一些细节,

  并下面这个JAR 文件:json-rpc-1.0.jar (75 kb)

  import org.json.JSONObject;

  ...

  ...

  JSONObject json = new JSONObject();

  json.put("city", "Mumbai");

  json.put("country", "India");

  ...

  String output = json.toString();

  ...

  10. 使用iText JAR生成PDF

  Java代码

  阅读这篇文章 了解更多细节

  import java.io.File;

  import java.io.FileOutputStream;

  import java.io.OutputStream;

  import java.util.Date;

  import com.lowagie.text.Document;

  import com.lowagie.text.Paragraph;

  import com.lowagie.text.pdf.PdfWriter;

  public class GeneratePDF {

  public static void main(String[] args) {

  try {

  OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));

  Document document = new Document();

  PdfWriter.getInstance(document, file);

  document.open();

  document.add(new Paragraph("Hello Kiran"));

  document.add(new Paragraph(new Date().toString()));

  document.close();

  file.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  11. HTTP 代理设置

  Java代码

  阅读这篇 文章 了解更多细节。

  System.getProperties().put("http.proxyHost", "someProxyURL");

  System.getProperties().put("http.proxyPort", "someProxyPort");

  System.getProperties().put("http.proxyUser", "someUserName");

  System.getProperties().put("http.proxyPassword", "somePassword");

  12. 单实例Singleton 示例

  Java代码

  请先阅读这篇文章 了解更多信息

  public class SimpleSingleton {

  private static SimpleSingleton singleInstance =  new SimpleSingleton();

  //Marking default constructor private

  //to avoid direct instantiation.

  private SimpleSingleton() {

  }

  //Get instance for class SimpleSingleton

  public static SimpleSingleton getInstance() {

  return singleInstance;

  }

  }

  另一种实现

  public enum SimpleSingleton {

  INSTANCE;

  public void doSomething() {

  }

  }

  //Call the method from Singleton:

  SimpleSingleton.INSTANCE.doSomething();

  13. 抓屏程序

  Java代码

  阅读这篇文章 获得更多信息。

  import java.awt.Dimension;

  import java.awt.Rectangle;

  import java.awt.Robot;

  import java.awt.Toolkit;

  import java.awt.image.BufferedImage;

  import javax.imageio.ImageIO;

  import java.io.File;

  ...

  public void captureScreen(String fileName) throws Exception {

  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

  Rectangle screenRectangle = new Rectangle(screenSize);

  Robot robot = new Robot();

  BufferedImage image = robot.createScreenCapture(screenRectangle);

  ImageIO.write(image, "png", new File(fileName));

  }

  ...

  阅读这篇文章 获得更多信息。

  import java.awt.Dimension;

  import java.awt.Rectangle;

  import java.awt.Robot;

  import java.awt.Toolkit;

  import java.awt.image.BufferedImage;

  import javax.imageio.ImageIO;

  import java.io.File;

  ...

  public void captureScreen(String fileName) throws Exception {

  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

  Rectangle screenRectangle = new Rectangle(screenSize);

  Robot robot = new Robot();

  BufferedImage image = robot.createScreenCapture(screenRectangle);

  ImageIO.write(image, "png", new File(fileName));

  }

上一页  [1] [2] [3] [4] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训