⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 barcodeencoder.java

📁 著名IDAutomation公司的JAVA条码控件源码
💻 JAVA
字号:
//*****************************************************************
 //
 //  JAVA Source for com.idautomation.linear; 4.10
 //
 //  Copyright, IDAutomation.com, Inc. 2000-2004.
 //  All rights reserved.
 //
 //  http://www.IDAutomation.com/java/
 //
 //  NOTICE:
 //  You may incorporate our Source Code in your application
 //  only if you own a valid Java Developer License
 //  from IDAutomation.com, Inc. and the copyright notices
 //  are not removed from the source code.
 //
 //*****************************************************************

package com.idautomation.barcode.encoder;

public class BarCodeEncoder {

  String sFile;
  String sFormat;
  com.idautomation.barcode.linear.BarCode bc;
  public boolean result;

  public BarCodeEncoder(com.idautomation.barcode.linear.BarCode c,
                        String psFormat, String psFile) {

    sFormat = psFormat;
    sFile = psFile;
    bc = c;
    result = encode();
  }

  private boolean encode() {
    if (sFormat.toUpperCase().compareTo("GIF") == 0) {
      return saveToGIF();
    }
    if (sFormat.toUpperCase().compareTo("JPEG") == 0) {
      return saveToJPEG();
    }
    return false;
  }

  private boolean saveToGIF() {
    String v = java.lang.System.getProperty("java.version");
    if (v.indexOf("1.1") == 0) {
      return false;
    }
    try {

      // *** This method was added to auto size the images the first time ***
      if (bc.autoSize) {
        java.awt.image.BufferedImage imageTemp = new java.awt.image.
            BufferedImage(100, 100,
                          java.awt.image.BufferedImage.TYPE_BYTE_INDEXED);
        java.awt.Graphics imgTempGraphics = imageTemp.createGraphics();
        bc.paint(imgTempGraphics);
        bc.invalidate();
        imgTempGraphics.dispose();
      }

      //create bufferred image
      java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(bc.
          getSize().width, bc.getSize().height,
          java.awt.image.BufferedImage.TYPE_BYTE_INDEXED);
      //java.awt.Image image=c.createImage(c.getSize().width,c.getSize().height);
      java.awt.Graphics imgGraphics = image.createGraphics();

      bc.paint(imgGraphics);

      // open file
      java.io.File f = new java.io.File(sFile);
      f.delete();
      java.io.FileOutputStream of = new java.io.FileOutputStream(f);

      // encode buffered image to a gif
      GifEncoder encoder = new GifEncoder(image, of);
      encoder.encode();
      of.close();

    } catch (Exception e) {
      return false;
    }

    return true;
  }

  private boolean saveToJPEG() {
    String v = java.lang.System.getProperty("java.version");
    if (v.indexOf("1.1") == 0) {
      return false;
    }
    try {

      // *** This method was added to auto size the images the first time ***
      if (bc.autoSize) {
        bc.setSize(170, 90);
        java.awt.image.BufferedImage imageTemp = new java.awt.image.
            BufferedImage(bc.getSize().width, bc.getSize().height,
                          java.awt.image.BufferedImage.TYPE_BYTE_INDEXED);
        java.awt.Graphics imgTempGraphics = imageTemp.createGraphics();
        bc.paint(imgTempGraphics);
        bc.invalidate();
        imgTempGraphics.dispose();
      }

      // create bufferred image
      java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(bc.
          getSize().width, bc.getSize().height,
          java.awt.image.BufferedImage.TYPE_INT_RGB);
      java.awt.Graphics imgGraphics = image.createGraphics();
      bc.paint(imgGraphics);

      // open file
      java.io.File f = new java.io.File(sFile);
      f.delete();
      java.io.FileOutputStream of = new java.io.FileOutputStream(f);

      // encode buffered image to a jpeg
      com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.
          jpeg.JPEGCodec.createJPEGEncoder(of);

      //increase the JPEG quality to 100%
      com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder.
          getDefaultJPEGEncodeParam(image);
      param.setQuality(1.0F, true);
      encoder.setJPEGEncodeParam(param);
      encoder.encode(image, param);
      of.close();
    } catch (Exception e) {
      return false;
    }
    return true;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -