java图片处理,首先上传一个大图然后按照给定比例或大小获取图片

如题用java实现的

GraphicsMagick + im4java 可以实现 ,而且生成的图片质量很高,已经实践过了。

参考一下这个
http://my.oschina.net/u/1245614/blog/160051

1你是要做上传头像吗?如果是http://download.csdn.net/detail/quadrapop/6552799
(开源的、稍微改改就行)

2如果不是,
上传图片可以用jquery file upload(https://github.com/blueimp/jQuery-File-Upload/wiki)这里有java的例子代码,写好的

图片处理可以用java awt的image api

package com.test;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**

  • 图片缩放
  • @author db2admin
    *
    /
    public class ImageUtil {
    /
    *

    • Logger for this class */ private static final Logger logger = Logger.getLogger(ImageUtil.class);

    public void imageScale(String srcFilePath, String targetFilePath,
    int width, int height) {
    this.imageScale(new File(srcFilePath), new File(targetFilePath), width,
    height);
    }

    public void imageScale(File srcFile, File targetFile, int width, int height) {
    try {
    Image image = javax.imageio.ImageIO.read(srcFile);

        image = image.getScaledInstance(width, height,
                Image.SCALE_AREA_AVERAGING);
        // Make a BufferedImage from the Image.
        BufferedImage mBufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = mBufferedImage.createGraphics();            
    
        g2.drawImage(image, 0, 0, width, height, Color.white, null);
        g2.dispose();
    
        float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2,
                -0.125f, -0.125f, -0.125f, -0.125f };
        Kernel kernel = new Kernel(3, 3, kernelData2);
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        mBufferedImage = cOp.filter(mBufferedImage, null);
    
        File targetDir = targetFile.getParentFile();
        if (!targetDir.exists())
            targetDir.mkdirs();
    
        FileOutputStream out = new FileOutputStream(targetFile);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(mBufferedImage);
        out.close();
    } catch (Exception e) {
        logger.error(
                "imageScale(String, String, int, int) - 图片压缩出错 - srcFilePath="
                        + srcFile.getPath() + ", targeFilePath="
                        + targetFile.getPath() + ", width=" + width
                        + ", height=" + height, e);
    }
    

    }

    public void imageScale(FileItem fileItem, String targetFilePath, int width,
    int height) {
    try {
    InputStream input = fileItem.getInputStream();
    Image image = javax.imageio.ImageIO.read(input);

        image = image.getScaledInstance(width, height,
                Image.SCALE_AREA_AVERAGING);
        BufferedImage mBufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = mBufferedImage.createGraphics();
    
        g2.drawImage(image, 0, 0, width, height, Color.white, null);
        g2.dispose();
    
        float[] kernelData2 = { -0.125f, -0.125f, -0.125f, -0.125f, 2,
                -0.125f, -0.125f, -0.125f, -0.125f };
        Kernel kernel = new Kernel(3, 3, kernelData2);
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        mBufferedImage = cOp.filter(mBufferedImage, null);
    
        File target = new File(targetFilePath);
        File targetDir = target.getParentFile();
        if (!targetDir.exists())
            targetDir.mkdirs();
    
        FileOutputStream out = new FileOutputStream(targetFilePath);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(mBufferedImage);
        out.close();
        input.close();
    } catch (Exception e) {
        logger.error(
                "imageScale(String, String, int, int) - 图片压缩出错 - fileItem="
                        + fileItem.getName() + ", targetFilePath="
                        + targetFilePath + ", width=" + width + ", height="
                        + height, e);
    }
    

    }

    public static void main(String[] args) {
    ImageUtil iu=new ImageUtil();
    iu.imageScale("e:/2.jpg", "e:/1.jpg", 270, 220);
    }
    }

看看有没有用