Java 中图片自适应处理

背景:原始图片宽高比例不能适应 新的页面需要,需要重新调整, 而目前除了使用 专业的图片工具之外, 也可以使用Java 自动处理,处理后的效果同 photoshop 几乎没有区别.

代码片段: 

BufferedImage in = ImageIO.read(new URL(url)); 

//使用3字节图片编码 
BufferedImage ut = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);// TYPE_4BYTE_BGR); 
Graphics g = out.getGraphics(); 

//填白底 
g.setColor(Color.white); 
g.fillRect(0, 0, w, h); 

int h1 = in.getHeight(); 
int w1 = in.getWidth(); 
float fh = ((float) h1) / h; 
float fw = ((float) w1) / w; 
int h = 0; 
int w = 0; 

if (fh > fw) { 
int w2 = (int) (w1/fh); 
ow = (w - w2)/2; 
w = w2; 
} else { 
int h2 = (int) (h1/fw); 
oh = (h - h2)/2; 
h = h2; 


//按比例缩/放原始图片 
Image tmp = in.getScaledInstance(w, h, Image.SCALE_SMOOTH); 
g.drawImage(tmp, ow, oh, w, h, null); 
ImageIO.write(out, "jpg", new File(file)); 
要实现图片自适应按钮大小且使得图片透明的区域为透明,可以使用Java的Swing库的JButton和ImageIcon类。 首先创建一个JButton对象,然后使用ImageIcon类将图片加载到JButton上。接着可以使用getPreferredSize()方法获取图片的实际大小,然后将JButton的大小设置为图片的大小,从而实现图片自适应按钮大小的效果。 为了使得图片透明的区域为透明,需要对图片进行处理。可以使用Java的BufferedImage类,将图片转换为可编辑的BufferedImage对象。然后遍历所有像素,将图片透明的部分的像素的Alpha值设置为0,从而使得透明的区域为透明。 下面是一个示例代码: ``` import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; public class TransparentButton extends JButton { private BufferedImage image; public TransparentButton(String imagePath) { try { // Load the image as a BufferedImage image = ImageIO.read(new File(imagePath)); // Make the button the same size as the image setPreferredSize(new Dimension(image.getWidth(), image.getHeight())); // Make the transparent parts of the image transparent in the button makeTransparent(); } catch (IOException e) { e.printStackTrace(); } } private void makeTransparent() { // Create a new BufferedImage that is compatible with the current screen BufferedImage bi = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); // Draw the image onto the new BufferedImage Graphics g = bi.getGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); // Loop through all the pixels in the image for (int x = 0; x < bi.getWidth(); x++) { for (int y = 0; y < bi.getHeight(); y++) { // Get the color of the pixel Color c = new Color(bi.getRGB(x, y), true); // If the alpha component of the color is 0 (transparent), set the alpha component to 0 if (c.getAlpha() == 0) { c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 0); bi.setRGB(x, y, c.getRGB()); } } } // Set the button's icon to the new BufferedImage setIcon(new ImageIcon(bi)); } public static void main(String[] args) { JFrame frame = new JFrame("Transparent Button Test"); TransparentButton button = new TransparentButton("image.png"); frame.getContentPane().add(button); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } ``` 在上面的代码,我们创建了一个继承自JButton的类TransparentButton,它从文件系统加载了一张图片,将图片透明的区域处理为透明,并将处理后的图片设置为按钮的图标。在main()方法,我们创建了一个JFrame,并将TransparentButton添加到JFrame。当运行程序时,我们会看到一个自适应大小的按钮,其图片的透明部分是透明的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值