一张超过2MB  小于10的图片加水印 在不修改原图大小的属性的情况下用Java后台实现在图片指定像素点上加水印
Java code: 
try { 
                        File file = new File(path);
                        Image src = ImageIo.read(file);      //程序运行到这里就报错啦
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(height, width,
BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
g.setColor(color);
g.setFont(new Font(fontName, fontStyle, fontSize));
String[] strArray = pressText.split("\n");
for (int i = 0; i < strArray.length; i++) {
int ySize = y + i * lineHeight;
g.drawString(strArray[i], x, ySize);
}
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", file);
} catch (Exception e) {
e.printStackTrace();
}
Exception code:
 java.lang.OutOfMemoryError: Java heap space
java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
java.awt.image.Raster.createPackedRaster(Raster.java:458)
java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
java.awt.image.BufferedImage.<init>(BufferedImage.java:312)
com.app.util.ImageUtils.createMark(ImageUtils.java:205)
com.app.service.InfringeCurrentManager.createMark(InfringeCurrentManager.java:109)
com.app.service.InfringeCurrentManager$$FastClassByCGLIB$$5168867b.invoke(<generated>)
net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
com.app.service.InfringeCurrentManager$$EnhancerByCGLIB$$9e281f02.createMark(<generated>)
com.app.web.InfringeCurrentAction.save(InfringeCurrentAction.java:221)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
经测试图片小于1MB的可以运行正常,file文件保证绝对存在,请那位高手解决一下!

解决方案 »

  1.   


    package com.jungle;import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;/**
     * 结图片添加水印
     */
    public class WaterStamp { public WaterStamp() {

    } /** */
    /**
     * 把图片印刷到图片上
     * 
     * @param pressImg --
     *            水印文件
     * @param targetImg --
     *            目标文件
     * @param x
     * @param y
     */
    public final static void pressImage(String pressImg, String targetImg,
    int x, int y) {
    try {
    File _file = new File(targetImg);
    Image src = ImageIO.read(_file);
    int wideth = src.getWidth(null);
    int height = src.getHeight(null);
    BufferedImage image = new BufferedImage(wideth, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics g = image.createGraphics();
    g.drawImage(src, 0, 0, wideth, height, null); // 水印文件
    File _filebiao = new File(pressImg);
    Image src_biao = ImageIO.read(_filebiao);
    int wideth_biao = src_biao.getWidth(null);
    int height_biao = src_biao.getHeight(null);
    g.drawImage(src_biao, wideth - wideth_biao - x, height
    - height_biao - y, wideth_biao, height_biao, null);
    // /
    g.dispose();
    FileOutputStream out = new FileOutputStream(targetImg);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(image);
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /** */
    /**
     * 打印文字水印图片
     * 
     * @param pressText
     *            --文字
     * @param targetImg --
     *            目标图片
     * @param fontName --
     *            字体名
     * @param fontStyle --
     *            字体样式
     * @param color --
     *            字体颜色
     * @param fontSize --
     *            字体大小
     * @param x --
     *            偏移量
     * @param y
     */ public static void pressText(String pressText, String targetImg,
    String fontName, int fontStyle, int color, int fontSize, int x,
    int y) {
    try {
    File _file = new File(targetImg);
    Image src = ImageIO.read(_file);
    int wideth = src.getWidth(null);
    int height = src.getHeight(null);
    BufferedImage image = new BufferedImage(wideth, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics g = image.createGraphics();
    g.drawImage(src, 0, 0, wideth, height, null);
    // String s="www.qhd.com.cn";
    g.setColor(Color.RED);
    g.setFont(new Font(fontName, fontStyle, fontSize)); g.drawString(pressText, wideth - fontSize - x, height - fontSize
    / 2 - y);
    g.dispose();
    FileOutputStream out = new FileOutputStream(targetImg);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(image);
    out.close();
    } catch (Exception e) {
    System.out.println(e);
    }
    } public static void main(String[] args) {
    pressImage("E:/images/ME/waterTest.jpg", "E:/images/2.jpg", 20, 20);
    }
    }
    网上找的!
      

  2.   

    抄的,你把这个问题给解决啦你就厉害撒,NND,哥是叫你解决问题的,不是叫你吹水的
      

  3.   

    抄的,你把这个问题给解决啦你就厉害撒,NND,哥是叫你解决问题的,不是叫你吹水的
      

  4.   

    是一张  2~10MB 的图片文件,路径保证没错,能解决嘛?我知道是内存溢出  我想知道的是Java对象内存溢出还是vim不够
      

  5.   

    调整 JVM -Xms -Xmx的值后,你再看看。