当前位置:编程学习 > JAVA >>

Java能切割别拉伸图片吗

按4个点切割  并拉伸成长方形 --------------------编程问答-------------------- 当然可以啊。。。


package com.imagecut;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageCut {

/**
 * @param args
 */
public static void main(String[] args) {
try {
ImageCut.cutImage("D:\\app_work\\eclipse_work\\myeclipse6.0.1_workspace\\chenwrite\\WebRoot\\imageCut\\img\\a.jpg", 300,300,70,20, 150, 150);
} catch (IOException e) {
e.printStackTrace();
}
}


/**
 * 生成缩放后的图片和剪裁后的图片
 * @param srcPath 原图片的路径
 * @param scale 缩放比例
 * @param startX 剪裁框的起始坐标 X
 * @param startY 剪裁框的起始坐标 Y
 * @param width 剪裁后的图片宽度
 * @param height 剪裁后的图片高度
 * @throws IOException IOException
 */
public static void cutImage(String srcPath, double scale, int startX, int startY, int width, int height) throws IOException {
File srcFile = new File(srcPath);
    BufferedImage image = ImageIO.read(srcFile);
    int srcWidth = image.getWidth(null);
    int srcHeight = image.getHeight(null);
cutImage(srcPath, (int)(srcWidth * scale), (int)(srcHeight * scale), startX, startY, width, height);
}

/**
 * 生成缩放后的图片和剪裁后的图片
 * @param srcPath 原图片的路径
 * @param scaleWidth 缩放后的图片宽度度
 * @param scaleHeight 缩放后的图片高度
 * @param startX 剪裁框的起始坐标 X
 * @param startY 剪裁框的起始坐标 Y
 * @param width 剪裁后的图片宽度
 * @param height 剪裁后的图片高度
 * @throws IOException IOException
 */
public static void cutImage(String srcPath, int scaleWidth, int scaleHeight, int startX, int startY, int width, int height) throws IOException {      
    File srcFile = new File(srcPath);       
    BufferedImage image = ImageIO.read(srcFile);       
    BufferedImage newImage = new BufferedImage(scaleWidth, scaleHeight, BufferedImage.TYPE_INT_RGB);       
    newImage.getGraphics().drawImage(image.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH), 0, 0, null);       
    //保存缩放后的图片
    String fileName = srcFile.getName().substring(0,srcFile.getName().lastIndexOf("."));       
    String fileSufix = srcFile.getName().substring(srcFile.getName().lastIndexOf(".") + 1);       
    File scaleFile = new File(srcFile.getParent(), fileName + "_scale" + "." + fileSufix);       
    ImageIO.write(newImage, fileSufix, scaleFile);       
     //保存裁剪后的图片
    File scaleCutFile = new File(srcFile.getParent(), fileName + "_scale_cut" + "." + fileSufix);  
    ImageIO.write(newImage.getSubimage(startX, startY, width, height), fileSufix, scaleCutFile);
}
}















































/**
public static void cutImage(String srcPath, int width, int height) throws IOException {      
    File srcFile = new File(srcPath);       
    BufferedImage image = ImageIO.read(srcFile);       
    int srcWidth = image.getWidth(null);       
    int srcHeight = image.getHeight(null);       
    int newWidth = 0, newHeight = 0;       
    int x = 0, y = 0;       
    double scale_w = (double)width/srcWidth;       
    double scale_h = (double)height/srcHeight;       
    System.out.println("scale_w="+scale_w+",scale_h="+scale_h);       
    //按原比例缩放图片       
    if(scale_w < scale_h) {       
        newHeight = height;       
        newWidth = (int)(srcWidth * scale_h);       
        x = (newWidth - width)/2;       
    } else {       
        newHeight = (int)(srcHeight * scale_w);       
        newWidth = width;       
        y = (newHeight - height)/2;       
    }       
    BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);       
    newImage.getGraphics().drawImage(       
    image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);       
    // 保存缩放后的图片      
    String fileSufix = srcFile.getName().substring(srcFile.getName().lastIndexOf(".") + 1);       
    File destFile = new File(srcFile.getParent(), UUID.randomUUID().toString() + "." + fileSufix);       
    // ImageIO.write(newImage, fileSufix, destFile);       
    // 保存裁剪后的图片       
    ImageIO.write(newImage.getSubimage(x, y, width, height), fileSufix, destFile);       
}
 */


最后一个方法中,有两个步骤:1做图片缩放,2做图片切割。。。你也可以换个位置,先切割,在缩放。。。
--------------------编程问答-------------------- 吖。。。忘记把注释的废弃代码删掉了。。。sorry --------------------编程问答-------------------- 4个点是不规则四边形
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,