「java图片切割」java图片裁剪

博主:adminadmin 2022-12-17 16:54:10 59

今天给各位分享java图片切割的知识,其中也会对java图片裁剪进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA PNG图片分割,无背景。

怎么会无法呢。java支持图片格式中最好的就是png,别的图片可以不支持,png是默认支持的。用ARGB色彩模型直接对png操作即可,

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Test {

static public void main(String 参数[]){

try{

BufferedImage img=ImageIO.read(new File("test.png"));

int half_w=img.getWidth()/2;

int rgb[]=new int[half_w*img.getHeight()];

img.getRGB(0, 0, half_w, img.getHeight(), rgb, 0, half_w);

BufferedImage img_half=new BufferedImage(half_w, img.getHeight(), BufferedImage.TYPE_INT_ARGB);

img_half.setRGB(0, 0,half_w,img.getHeight(), rgb,0,half_w);

//保存到新文件half.png里面

ImageIO.write(img_half,"PNG",new File("half.png"));

}catch (IOException e){

e.printStackTrace();

}

}

}

======

得到half.png签名图的左半边,保留了透明的背景。

这已经只有5-6行,抛砖引玉,用raster可能代码更简..

如何用java实现切割一张图片

BufferedImage类有一个getSubimage()方法,以下来自API

public BufferedImage getSubimage(int x,

int y,

int w,

int h)

返回由指定矩形区域定义的子图像。返回的 BufferedImage 与源图像共享相同的数据数组。

参数:

x - 指定矩形区域左上角的 X 坐标

y - 指定矩形区域左上角的 Y 坐标

w - 指定矩形区域的宽度

h - 指定矩形区域的高度

返回:

BufferedImage,它是此 BufferedImage 的子图像。

抛出:

RasterFormatException - 如果指定区域不包含在此 BufferedImage 中

java编程怎么才能分割图片

public BufferedImage getSubimage(int x,

int y,

int w,

int h)返回由指定矩形区域定义的子图像。返回的 BufferedImage 与源图像共享相同的数据数组。

参数:

x - 指定矩形区域左上角的 X 坐标

y - 指定矩形区域左上角的 Y 坐标

w - 指定矩形区域的宽度

h - 指定矩形区域的高度

你先把分块的坐标弄好,在拿这个方法去拿没块的图就是了。

如何在Java中进行图片剪裁 疯狂JAVA

package test;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.geom.AffineTransform;

import java.awt.image.AffineTransformOp;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.nio.Buffer;

import javax.imageio.ImageIO;

import javax.imageio.stream.ImageOutputStream;

/**

 * 裁剪、缩放图片工具类

 * 

 * @author CSDN 没有梦想-何必远方

 */

public class ImgUtils {

/**

 * 缩放图片方法

 * 

 * @param srcImageFile

 *            要缩放的图片路径

 * @param result

 *            缩放后的图片路径

 * @param height

 *            目标高度像素

 * @param width

 *            目标宽度像素

 * @param bb

 *            是否补白

 */

public final static void scale(String srcImageFile, String result,

int height, int width, boolean bb) {

try {

double ratio = 0.0; // 缩放比例

File f = new File(srcImageFile);

BufferedImage bi = ImageIO.read(f);

Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);// bi.SCALE_SMOOTH

// 选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。

// 计算比例

if ((bi.getHeight()  height) || (bi.getWidth()  width)) {

double ratioHeight = (new Integer(height)).doubleValue()

/ bi.getHeight();

double ratioWhidth = (new Integer(width)).doubleValue()

/ bi.getWidth();

if (ratioHeight  ratioWhidth) {

ratio = ratioHeight;

} else {

ratio = ratioWhidth;

}

AffineTransformOp op = new AffineTransformOp(AffineTransform// 仿射转换

.getScaleInstance(ratio, ratio), null);// 返回表示剪切变换的变换

itemp = op.filter(bi, null);// 转换源 BufferedImage 并将结果存储在目标

// BufferedImage 中。

}

if (bb) {// 补白

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);// 构造一个类型为预定义图像类型之一的

// BufferedImage。

Graphics2D g = image.createGraphics();// 创建一个

// Graphics2D,可以将它绘制到此

// BufferedImage 中。

g.setColor(Color.white);// 控制颜色

g.fillRect(0, 0, width, height);// 使用 Graphics2D 上下文的设置,填充 Shape

// 的内部区域。

if (width == itemp.getWidth(null))

g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,

itemp.getWidth(null), itemp.getHeight(null),

Color.white, null);

else

g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,

itemp.getWidth(null), itemp.getHeight(null),

Color.white, null);

g.dispose();

itemp = image;

}

ImageIO.write((BufferedImage) itemp, "JPEG", new File(result)); // 输出压缩图片

} catch (IOException e) {

e.printStackTrace();

}

}

/**

 * 裁剪图片方法

 * 

 * @param bufferedImage

 *            图像源

 * @param startX

 *            裁剪开始x坐标

 * @param startY

 *            裁剪开始y坐标

 * @param endX

 *            裁剪结束x坐标

 * @param endY

 *            裁剪结束y坐标

 * @return

 */

public static BufferedImage cropImage(BufferedImage bufferedImage,

int startX, int startY, int endX, int endY) {

int width = bufferedImage.getWidth();

int height = bufferedImage.getHeight();

if (startX == -1) {

startX = 0;

}

if (startY == -1) {

startY = 0;

}

if (endX == -1) {

endX = width - 1;

}

if (endY == -1) {

endY = height - 1;

}

BufferedImage result = new BufferedImage(endX - startX, endY - startY,

4);

for (int x = startX; x  endX; ++x) {

for (int y = startY; y  endY; ++y) {

int rgb = bufferedImage.getRGB(x, y);

result.setRGB(x - startX, y - startY, rgb);

}

}

return result;

}

public static void main(String[] args) throws IOException {

File input = new File("input.jpg");

BufferedImage img = ImageIO.read(input);

cropImage(img, 10, 10, 20, 20);

File output = new File("output.jpg");

ImageIO.write(img, "jpg", output);

}

}

求一个Java切割图片的函数

package com.supben.util;

import java.awt.Dimension;

import java.awt.Image;

import java.awt.Point;

import java.awt.Rectangle;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Iterator;

import javax.imageio.ImageIO;

import javax.imageio.ImageReadParam;

import javax.imageio.ImageReader;

import javax.imageio.stream.ImageInputStream;

import org.apache.log4j.Logger;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGEncodeParam;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {

private static final Logger log = Logger.getLogger(ImageUtil.class);

/**

* 切割图片

* @param x 截点横坐标 (从左开始计数)

* @param y 截点纵坐标 (从上开始计数)

* @param width 截取的宽度

* @param height 截取的长度

* @param oldpath 图片位置

* @param newpath 新生成的图片位置

*/

public static void cutImage(int x, int y, int width, int height, String oldpath, String newpath) {

FileInputStream is = null;

ImageInputStream iis = null;

//这个是获取图片扩展名的方法,比如:jpg。我这里有现成的,如果没有,自己实现

String imgType = StringUtil.getExt(oldpath);

try {

is = new FileInputStream(oldpath);

IteratorImageReader it = ImageIO.getImageReadersByFormatName(imgType);

ImageReader reader = it.next();

iis = ImageIO.createImageInputStream(is);

reader.setInput(iis, true);

ImageReadParam param = reader.getDefaultReadParam();

Point p = new Point();

p.setLocation(x, y);

Dimension d = new Dimension();

d.setSize(width, height);

Rectangle rect = new Rectangle(p, d);

param.setSourceRegion(rect);

BufferedImage bi = reader.read(0, param);

ImageIO.write(bi, imgType, new File(newpath));

is.close();

iis.close();

} catch (Exception e) {

log.error(e);

}

}

/**

* 缩略图片

* @param oldpath 原图片

* @param newpath 新生成的图片存放地址

* @param wdith 缩略后的宽

* @param height 缩略后的高

*/

public static void scaleImage(String oldpath, String newpath, int wdith, int height) {

// 获取老的图片

File oldimg = new File(oldpath);

try {

BufferedImage bi = ImageIO.read(oldimg);

Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH);

BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB);

thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);

// 缩略后的图片路径

File newimg = new File(newpath);

FileOutputStream out = new FileOutputStream(newimg);

// 绘图

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail);

param.setQuality(1.0f, false);

encoder.encode(thumbnail);

out.close();

bi.flush();

bi = null;

} catch (IOException e) {

log.error(e);

}

}

public static void main(String[] args) {

scaleImage("D:/2.jpg", "D:/3.jpg", 50, 50);

}

}

关于java图片切割和java图片裁剪的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-17,除非注明,否则均为首码项目网原创文章,转载请注明出处。