bmp直接显示或转为jpg

JDk自身的几个类JDialog,JTextPane,JLable等都不支持直接显示bmp,如果图片是bmp的就没办法了,今天一直在搞这个,分析bmp的文件格式扩展以上的几个类把它显示出来,也可用于转换为jpg格式,然后再处理。

/**
 * <p>Title:BmpReader</p>
 * <p>Copyright:Copyright (c) 2005</p>
 * <p>Company:www.b9527.net</p>
 * @author robertb9527
 * @version 1.0
 */

 

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;

public class BmpReader {
  public void reader(String file) {
    try {
      FileInputStream in = new FileInputStream(file);
      Image TheImage = read(in);
      int wideth = TheImage.getWidth(null);
      int height = TheImage.getHeight(null);
      BufferedImage tag = new BufferedImage(wideth / 2, height / 2,
                                            BufferedImage.TYPE_INT_RGB);
      tag.getGraphics().drawImage(TheImage, 0, 0, wideth / 2, height / 2, null);
      FileOutputStream out = new FileOutputStream(file + ".jpg");
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
      encoder.encode(tag);
      out.close();
    }
    catch (Exception e) {
      System.out.println(e);
    }
  }

  public static int constructInt(byte[] in, int offset) {
    int ret = ( (int) in[offset + 3] & 0xff);
    ret = (ret << 8) | ( (int) in[offset + 2] & 0xff);
    ret = (ret << 8) | ( (int) in[offset + 1] & 0xff);
    ret = (ret << 8) | ( (int) in[offset + 0] & 0xff);
    return (ret);
  }

  public static int constructInt3(byte[] in, int offset) {
    int ret = 0xff;
    ret = (ret << 8) | ( (int) in[offset + 2] & 0xff);
    ret = (ret << 8) | ( (int) in[offset + 1] & 0xff);
    ret = (ret << 8) | ( (int) in[offset + 0] & 0xff);
    return (ret);
  }

  public static long constructLong(byte[] in, int offset) {
    long ret = ( (long) in[offset + 7] & 0xff);
    ret |= (ret << 8) | ( (long) in[offset + 6] & 0xff);
    ret |= (ret << 8) | ( (long) in[offset + 5] & 0xff);
    ret |= (ret << 8) | ( (long) in[offset + 4] & 0xff);
    ret |= (ret << 8) | ( (long) in[offset + 3] & 0xff);
    ret |= (ret << 8) | ( (long) in[offset + 2] & 0xff);
    ret |= (ret << 8) | ( (long) in[offset + 1] & 0xff);
    ret |= (ret << 8) | ( (long) in[offset + 0] & 0xff);
    return (ret);
  }

  public static double constructDouble(byte[] in, int offset) {
    long ret = constructLong(in, offset);
    return (Double.longBitsToDouble(ret));
  }

  public static short constructShort(byte[] in, int offset) {
    short ret = (short) ( (short) in[offset + 1] & 0xff);
    ret = (short) ( (ret << 8) | (short) ( (short) in[offset + 0] & 0xff));
    return (ret);
  }

  static class BitmapHeader {
    public int iSize, ibiSize, iWidth, iHeight, iPlanes, iBitcount,
        iCompression, iSizeimage, iXpm, iYpm, iClrused, iClrimp;
    // 读取bmp文件头信息
    public void read(FileInputStream fs) throws IOException {
      final int bflen = 14;
      byte bf[] = new byte[bflen];
      fs.read(bf, 0, bflen);
      final int bilen = 40;
      byte bi[] = new byte[bilen];
      fs.read(bi, 0, bilen);
      iSize = constructInt(bf, 2);
      ibiSize = constructInt(bi, 2);
      iWidth = constructInt(bi, 4);
      iHeight = constructInt(bi, 8);
      iPlanes = constructShort(bi, 12);
      iBitcount = constructShort(bi, 14);
      iCompression = constructInt(bi, 16);
      iSizeimage = constructInt(bi, 20);
      iXpm = constructInt(bi, 24);
      iYpm = constructInt(bi, 28);
      iClrused = constructInt(bi, 32);
      iClrimp = constructInt(bi, 36);
    }
  }

  public static Image read(FileInputStream fs) {
    try {
      BitmapHeader bh = new BitmapHeader();
      bh.read(fs);
      if (bh.iBitcount == 24) {
        return (readImage24(fs, bh));
      }
      if (bh.iBitcount == 32) {
        return (readImage32(fs, bh));
      }
      fs.close();
    }
    catch (IOException e) {
      System.out.println(e);
    }
    return (null);
  }

  //24位
  protected static Image readImage24(FileInputStream fs, BitmapHeader bh) throws
      IOException {
    Image image;
    if (bh.iSizeimage == 0) {
      bh.iSizeimage = ( ( ( (bh.iWidth * bh.iBitcount) + 31) & ~31) >> 3);
      bh.iSizeimage *= bh.iHeight;
    }
    int npad = (bh.iSizeimage / bh.iHeight) - bh.iWidth * 3;
    int ndata[] = new int[bh.iHeight * bh.iWidth];
    byte brgb[] = new byte[ (bh.iWidth + npad) * 3 * bh.iHeight];
    fs.read(brgb, 0, (bh.iWidth + npad) * 3 * bh.iHeight);
    int nindex = 0;
    for (int j = 0; j < bh.iHeight; j++) {
      for (int i = 0; i < bh.iWidth; i++) {
        ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(
            brgb, nindex);
        nindex += 3;
      }
      nindex += npad;
    }
    image = Toolkit.getDefaultToolkit().createImage
        (new MemoryImageSource(bh.iWidth, bh.iHeight,
                               ndata, 0, bh.iWidth));
    fs.close();
    return (image);
  }

//32位
  protected static Image readImage32(FileInputStream fs, BitmapHeader bh) throws
      IOException {
    Image image;
    int xwidth = bh.iSizeimage / bh.iHeight;
    int ndata[] = new int[bh.iHeight * bh.iWidth];
    byte brgb[] = new byte[bh.iWidth * 4 * bh.iHeight];
    fs.read(brgb, 0, bh.iWidth * 4 * bh.iHeight);
    int nindex = 0;
    for (int j = 0; j < bh.iHeight; j++) {
      for (int i = 0; i < bh.iWidth; i++) {
        ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(
            brgb, nindex);
        nindex += 4;
      }
    }
    image = Toolkit.getDefaultToolkit().createImage
        (new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0, bh.iWidth));
    fs.close();
    return (image);
  }

  public static Image load(String sdir, String sfile) {
    return (load(sdir + sfile));
  }

  public static Image load(String sdir) {
    try {
      FileInputStream fs = new FileInputStream(sdir);
      return (read(fs));
    }
    catch (IOException ex) {
      return (null);
    }
  }

  public ImageDialog getBmpImage(String filePath) throws IOException {
    if (filePath.equals("")) {
      System.out.println("输入bmp文件名");
      return null;
    }
    else {
      FileInputStream in = new FileInputStream(filePath);
      Image TheImage = read(in);
      ImageDialog container = new ImageDialog(new ImageIcon(TheImage));
      return container;
    }
  }

  public ImageIcon getBmpImageIcon(String filePath) throws IOException {
    if (filePath.equals("")) {
      System.err.println("输入bmp文件名");
      return null;
    }
    else {
      FileInputStream in = new FileInputStream(filePath);
      Image TheImage = read(in);
      return new ImageIcon(TheImage);
    }
  }
}

 

/**
 * <p>Title:ImageDialog </p>
 * <p>Copyright:Copyright (c) 2005</p>
 * <p>Company:www.b9527.net </p>
 * @author robertb9527
 * @version 1.0
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ImageDialog
    extends JDialog {
  JPanel jPanel = new JPanel();
  JLabel jLabel;
  ImageIcon imageURL = new ImageIcon();
  BorderLayout theBorderLayout = new BorderLayout();

  public ImageDialog(Frame frame, String title, boolean modal, String name) {
    super(frame, title, modal);
    try {
      this.imageURL = new ImageIcon(name);
      jbInit();
      pack();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public ImageDialog() {
    this(null, "", false, null);
  }

  public ImageDialog(String name) {
    this(null, "", false, name);
  }

  public ImageDialog(ImageIcon example) {
    super();
    this.imageURL = example;
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    pack();
  }

  private void jbInit() throws Exception {
    jPanel.setLayout(theBorderLayout);
    this.getContentPane().add(jPanel);
    jLabel = new JLabel();
    jLabel.setIcon(imageURL);
    jPanel.add(jLabel, BorderLayout.NORTH);
  }

  public ImageIcon getImage() {
    return this.imageURL;
  }

  public void setImage(ImageIcon example) {
    this.imageURL = example;
  }

  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      cancel();
    }
    super.processWindowEvent(e);
  }

  void cancel() {
    dispose();
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值