「java.切割」java 切割
今天给各位分享java.切割的知识,其中也会对java 切割进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java能切割别拉伸图片吗
当然可以啊。。。
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做图片切割。。。你也可以换个位置,先切割,在缩放。。。
如何用Java来进行文件切割和简单的内容过滤
java中有一个FilenameFilter的接口,能够过滤得到指定类型的文件或者目录,可以实现文件过滤器,如下代码: accept(File file,String path)方法public class DirFilter implements FilenameFilter{private String type;public DirFilter(Strin
java文件字符流这么写代码需要切割
需要。java文件字符流这么写代码需要切割,将一个比较大的文件切割成多个碎片文件,文件切割有2中方式。
java如何进行字符串切割提取
运用正则表达式分割就可以了,测试代码
public class Test {
public static void main(String[] args) {
String str="how|are|you";
String[] ary = str.split("\\|");
String s1 = ary[0];
String s2 = ary[1];
String s3 = ary[2];
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
System.out.println("s3 = " + s3);
}
}
----------测试结果
s1 = how
s2 = are
s3 = you
java切割字符串需求如下
按照你的要求切割字符串的Java程序如下
public class A{
public static void main(String[] args){
String str="img/a123.jpg,img/b123.jpg,img/c123.jpg";
String[] s=str.split(",");
for(int i=0;is.length;i++){
System.out.println(s[i]);
}
}
}
关于java.切割和java 切割的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。