Image转BufferedImage
来源:互联网 发布:淘宝店铺转让合同 编辑:程序博客网 时间:2024/05/22 05:25
- import java.awt.Graphics;
- import java.awt.GraphicsConfiguration;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.HeadlessException;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.Transparency;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- public class TestImage {
- public static void main(String[] args) throws IOException{
- Image img = Toolkit.getDefaultToolkit().getImage("C:\\google.jpg");
- BufferedImage bi_scale = toBufferedImage(img);
- ImageIO.write(bi_scale, "jpg",new File("C:\\2.jpg"));
- }
- public static BufferedImage toBufferedImage(Image image) {
- if (image instanceof BufferedImage) {
- return (BufferedImage)image;
- }
- // This code ensures that all the pixels in the image are loaded
- image = new ImageIcon(image).getImage();
- // Determine if the image has transparent pixels; for this method's
- // implementation, see e661 Determining If an Image Has Transparent Pixels
- //boolean hasAlpha = hasAlpha(image);
- // Create a buffered image with a format that's compatible with the screen
- BufferedImage bimage = null;
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
- try {
- // Determine the type of transparency of the new buffered image
- int transparency = Transparency.OPAQUE;
- /* if (hasAlpha) {
- transparency = Transparency.BITMASK;
- }*/
- // Create the buffered image
- GraphicsDevice gs = ge.getDefaultScreenDevice();
- GraphicsConfiguration gc = gs.getDefaultConfiguration();
- bimage = gc.createCompatibleImage(
- image.getWidth(null), image.getHeight(null), transparency);
- } catch (HeadlessException e) {
- // The system does not have a screen
- }
- if (bimage == null) {
- // Create a buffered image using the default color model
- int type = BufferedImage.TYPE_INT_RGB;
- //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
- /*if (hasAlpha) {
- type = BufferedImage.TYPE_INT_ARGB;
- }*/
- bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
- }
- // Copy image to buffered image
- Graphics g = bimage.createGraphics();
- // Paint the image onto the buffered image
- g.drawImage(image, 0, 0, null);
- g.dispose();
- return bimage;
- }
- }
阅读全文
0 0
- Image转BufferedImage
- Image转BufferedImage
- Image转BufferedImage
- Image转BufferedImage并提取像素进行颜色空间转换
- BufferedImage和Image的区别
- Java里将Image转换成BufferedImage
- Image抽象类和BufferedImage实现类
- Java加载图片 利用Image与BufferedImage
- Java加载图片 Image与BufferedImage 区别
- BufferedImage缩放Unknown image type 0
- java TookitImage转BufferedImage
- BufferedImage
- BufferedImage
- eclipse找不到import java.awt.image.BufferedImage这个类,调用不了bufferedimage
- 关于BufferedImage的使用(怎样用Image来绘制图形)
- Java swing 加载和显示图片 利用Image与BufferedImage
- linux Could not initialize class java.awt.image.BufferedImage
- java--BufferedImage 与byte[]的互转
- javaScript原型添加
- 机器学习----SVD
- Java 判断字符串是否是网址
- Python学习(1)
- 【深入浅出jQuery】源码浅析--整体架构
- Image转BufferedImage
- 未能加载文件或程序集“XXXX”或它的某一个依赖项。试图加载格式不正确的程序。(已解决)
- 资深首席架构师眼中的架构应该是什么样子的
- 动态规划编程实例——LeetCode 494.Target Sum
- Linux下修改Mysql的用户(root)的密码
- 不定宽高元素垂直左右居中
- oracle中添加、删除、修改字段sql
- Java Swing中文件选择器的使用
- iOS开发:集成支付宝