此解决方案使用 JAI (Java Advance Image) API
所有的魔法都在 QuadToQuad 方法中。这是代码示例。
try
{
BufferedImage img = UtilImageIO.loadImage(picName);
ParameterBlock params = new ParameterBlock();
params.addSource(img); //source is the input image
int w = img.getWidth(); //Set to the original width of the image
int h = img.getHeight(); //Set to the original height of image
Point tl = new Point(x,y); //The new top left corner
Point tr = new Point((x1,y1); //The new top right corner
Point bl = new Point(x2,y2); //The new bottom left corner
Point br = new Point(x3,y3); //The new bottom right corner
PerspectiveTransform p = PerspectiveTransform.getQuadToQuad(0,0, 0, h, w, h, w, 0, tl.x, tl.y, bl.x, bl.y, br.x, br.y, tr.x, tr.y).createInverse();
WarpPerspective wa = new WarpPerspective(p);
params.add(wa);
params.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC)); //Change the interpolation if you need more speed
RenderedOp dest = JAI.create("warp", params); //dest is now the output
File outputfile = new File(picName);
ImageIO.write(dest, "jpg", outputfile);
}
catch(Exception e){}
希望对您有所帮助。 :)