BufferedImage.setRGB(int startX,int startY,int w,int h,int[] rgbArray,int offset,int scansize)的用法

本文详细介绍了 Java 中 BufferedImage 类的使用方法,包括如何创建透明图片、从流中解码图片、设置记录等内容。通过多个示例代码片段展示了 setRGB 方法的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.javadocexamples.com/java/awt/image/BufferedImage/setRGB(int%20startX,int%20startY,int%20w,int%20h,int[]%20rgbArray,int%20offset,int%20scansize).html  

1: public class ColorPan extends JComponent {
   2:   BufferedImage image;
   3: 
   4:         ...
   5:     }
   6:     image = new BufferedImage(width, height,
   7:         ...
   8:         BufferedImage.TYPE_INT_RGB);
   9:         ...
  10:     image.setRGB(0, 0, width, height, data, 0, width);

View Full Code Here
   1: 
   2: import java.awt.image.BufferedImage;
   3: import java.util.Arrays;
   4:         ...
   5: 
   6:   public static BufferedImage createTransparentImage (final int width, final int height)
   7:   {
   8:         ...
   9:     final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  10:     final int[] data = img.getRGB(0, 0, width, height, null, 0, width);
  11:         ...
  12:     Arrays.fill(data, 0x00000000);
  13:     img.setRGB(0, 0, width, height, data, 0, width);

View Full Code Here
   1: 
   2: import java.awt.image.BufferedImage;
   3: import java.util.Arrays;
   4:         ...
   5: 
   6:   public static BufferedImage createTransparentImage (final int width, final int height)
   7:   {
   8:         ...
   9:     final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  10:     final int[] data = img.getRGB(0, 0, width, height, null, 0, width);
  11:         ...
  12:     Arrays.fill(data, 0x00000000);
  13:     img.setRGB(0, 0, width, height, data, 0, width);

View Full Code Here
   1: import javax.imageio.stream.ImageInputStream;
   2: import java.awt.image.BufferedImage;
   3: import java.awt.Dimension;
   4:         ...
   5: 
   6:     public BufferedImage decode(ImageInputStream in) throws IOException, BMPException {
   7:     skipToImage(in);
   8:         ...
   9:     int w = (int)d.getWidth();
  10:     BufferedImage image = new BufferedImage(w, h, 
  11:                         BufferedImage.TYPE_INT_RGB);
  12:         ...
  13:     }
  14:     image.setRGB(0, 0, w, h, data, 0, w);

View Full Code Here
   1: 
   2: import java.awt.image.BufferedImage;
   3: import java.io.IOException;
   4:         ...
   5: 
   6:   public BufferedImage setRecord (final MfRecord record)
   7:           throws IOException
   8:         ...
   9: 
  10:   public BufferedImage setRecord (final MfRecord record, final int offset)
  11:           throws IOException
  12:         ...
  13:     final BufferedImage retval = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  14:     retval.setRGB(0, 0, width, height, data, 0, width);

View Full Code Here
   1:     private Image logo;
   2:     private BufferedImage bi;
   3:     private int[] original;
   4:         ...
   5:         if ((modifier & InputEvent.BUTTON2_MASK) != 0) {
   6:             bi.setRGB(0, 0, w, h, original, 0, w);
   7:             repaint();
   8:         ...
   9:         }
  10:         bi.setRGB(0, 0, w, h, rgb, 0, w);
  11:         repaint();

View Full Code Here
   1: import java.awt.Image;
   2: import java.awt.image.BufferedImage;
   3: import java.awt.image.ImageObserver;
   4:         ...
   5:       long time = System.currentTimeMillis();
   6:       BufferedImage image;
   7:       int transferSize;
   8:         ...
   9:          {
  10:             image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  11:          }
  12:         ...
  13:          int[] img = (int[]) server.getImage(ID, RemoteImageServer.FAST_CONNECTION);
  14:          image.setRGB(0, 0, w, h, img, 0, w);

View Full Code Here
   1:     long duration;
   2:     BufferedImage im = null;
   3:     running = true;
   4:         ...
   5: 
   6:   private void analyzeImage(BufferedImage im)
   7:   {
   8:         ...
   9: 
  10:     im.setRGB(0, 0, imWidth, imHeight, pixels, 0, imWidth);

View Full Code Here
   1: import java.awt.event.KeyEvent;
   2: import java.awt.image.BufferedImage;
   3: import java.lang.reflect.Field;
   4:         ...
   5: 
   6:     private BufferedImage createTransparentImage(final int width, final int height) {
   7: #if USE_ORIGINAL_CODE
   8:         ...
   9:         final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  10:         final int[] data = img.getRGB(0, 0, width, height, null, 0, width);
  11:         ...
  12:         Arrays.fill(data, 0x00000000);
  13:         img.setRGB(0, 0, width, height, data, 0, width);

View Full Code Here
public static void createMarkForPic2(String sourcePath, String outputPath, String watermarkText, Color textColor, float opacity, String fontName, int fontSize, int spacing, Color borderColor, String fileExtension, Map<String, Object> params) { // 强制设置为20pt字体 fontSize = 20; try { // 读取原始图片 BufferedImage sourceImage = ImageIO.read(new File(sourcePath)); int width = sourceImage.getWidth(); int height = sourceImage.getHeight(); // 创建画布 BufferedImage watermarkedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = watermarkedImage.createGraphics(); g2d.drawImage(sourceImage, 0, 0, null); // 智能自适应方案 int baseSize = Math.min(width, height); int adaptiveFontSize = (int)(baseSize * 0.03); // 根据图片尺寸动态计算 fontSize = Math.max(20, adaptiveFontSize); // 至少20pt // 设置字体(加粗) Font font = new Font(fontName, Font.BOLD, fontSize); g2d.setFont(font); // 计算水印位置(智能居中偏左上) FontMetrics metrics = g2d.getFontMetrics(); int textWidth = metrics.stringWidth(watermarkText); int textHeight = metrics.getHeight(); int posX = width / 20; // 水平位置:1/20宽度处 int posY = textHeight + (height / 20); // 垂直位置:文字高度+1/20高度处 // 自适应透明度(深色背景用高透明度,浅色背景用低透明度) float adaptiveOpacity = calculateAdaptiveOpacity(sourceImage, posX, posY, textWidth, textHeight); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, adaptiveOpacity)); // 绘制水印(白字黑边) g2d.setColor(Color.BLACK); g2d.drawString(watermarkText, posX-1, posY-1); g2d.drawString(watermarkText, posX+1, posY-1); g2d.drawString(watermarkText, posX-1, posY+1); g2d.drawString(watermarkText, posX+1, posY+1); g2d.setColor(Color.WHITE); g2d.drawString(watermarkText, posX, posY); g2d.dispose(); // 保存图片 String formatName = fileExtension.startsWith(".") ? fileExtension.substring(1) : fileExtension; ImageIO.write(watermarkedImage, formatName, new File(outputPath)); } catch (Exception e) { e.printStackTrace(); } } // 智能计算背景区域平均亮度(用于确定最佳透明度) private static float calculateAdaptiveOpacity(BufferedImage image, int x, int y, int width, int height) { // 采样水印区域背景色 int sampleCount = 0; long totalBrightness = 0; int endX = Math.min(x + width, image.getWidth()); int endY = Math.min(y + height, image.getHeight()); for (int i = x; i < endX; i += 5) { // 每隔5像素采样 for (int j = y; j < endY; j += 5) { int rgb = image.getRGB(i, j); int r = (rgb >> 16) & 0xFF; int g = (rgb >> 8) & 0xFF; int b = rgb & 0xFF; totalBrightness += (r + g + b) / 3; sampleCount++; } } if (sampleCount == 0) return 0.7f; float avgBrightness = totalBrightness / (float)sampleCount; // 亮度越高(背景越亮),透明度越低(水印越明显) return avgBrightness > 150 ? 0.8f : avgBrightness > 100 ? 0.7f : 0.6f; }这段添加水印的代码需要修改为如果已存在水印就先清除水印再重新添加水印,避免重复添加水印导致水印重叠看不清,可以接受每次添加水印时如果识别到有水印存在,将水印区域设置成白色,再重新添加水印的方案,或者有其他更好的方案也行
07-12
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值