Java上传图片、剪裁图片、 imgareaselect +BufferedImage + BufferedImage
2017-02-22 17:18
351 查看
我当时用到的场景:有需求,上传用户头像,用户还可以自己裁剪图片。首先来个示例,然后开始正式进入主题。
前台:1.imageSelect.jsp
<body>
<script type="text/javascript">
function preview(img, selection) {
if (!selection.width || !selection.height)
return;
var scaleX = 100 / selection.width;
var scaleY = 100 / selection.height;
$('#preview img').css({
width : Math.round(scaleX * 300),
height : Math.round(scaleY * 300),
marginLeft : -Math.round(scaleX * selection.x1),
marginTop : -Math.round(scaleY * selection.y1)
});
}
$(function() {
$('#photo').imgAreaSelect({
aspectRatio : '1:1',
handles : true,
fadeSpeed : 200,
onSelectChange : preview
});
});
</script>
<div class="frame" style="width: 300px; height: 300px;float:left;margin-right:50px">
<img id="photo" src="files/flower2.jpg">
</div>
<div id="preview" style="width: 100px; height: 100px; overflow: hidden;">
<img src="files/flower2.jpg" style="width: 100px; height: 100px;">
</div>
</body>
后台:2.imageTool.java
public class ImgTool {
private BufferedImage subImg;
/**
* 截图
* @param srcPath
* @param startX
* @param startY
* @param width
* @param height
*/
public void cut(String srcPath,int startX,int startY,int width,int height){
try {
BufferedImage bufImg = ImageIO.read(new File(srcPath));
subImg = bufImg.getSubimage(startX, startY, width, height);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 保存截图。
* @param bufImg
* @param imgType
* @param tarPath
*/
public void save(String imgType,String imgName,String tarPath,int width,int height){
try {/**压缩图片为指定尺寸*/
if(subImg.getWidth()!=width || subImg.getHeight()!=height){
BufferedImage tempImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
tempImg.getGraphics().drawImage(subImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0,null);
ImageIO.write(tempImg, imgType, new File(tarPath+"/"+imgName));
}else{
ImageIO.write(subImg,imgType,new File(tarPath+"/"+imgName));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在正是开始了。这是我在项目中的写法,希望能对大家有帮助:
1.先将 BufferedImage转换成InputStream,将文件接收到并裁减成自己想要的大小规格。
2.然后完成后 InputStream转换成 BufferedImage
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(tempImg, "gif", os);
InputStream inputStrem = new ByteArrayInputStream(os.toByteArray());
3,。最后上传到ftp文件服务器:
fileMessage = sftpFileOper.upSftpFileCommon(filename, ins, path);//完成数据提交上传到ftp服务器
public JSONObject setUserHead(Map<String, Object> urlParams,Map<String, String> reqParams,
TuUser tuUser,TuAccountAvatarMapper avatarMpper){
JSONObject jsonObj = new JSONObject();
//reqParams里面获取的参数:startX,startY, width, height, imgName, imgType
//:String srcPath,int startX,int startY,int width,int height
//获取源文件流:String srcPath;
//配置文件中读取ftp上的保存路径:String tarPath
Map<String , Object> map = new HashMap<>();
MultipartFile[] files = (MultipartFile[]) urlParams.get("file");
int startX = Integer.parseInt(reqParams.get("startX"));
int startY = Integer.parseInt(reqParams.get("startY"));
int width = Integer.parseInt(reqParams.get("width"));
int height = Integer.parseInt(reqParams.get("height"));
String imgName = "";//图片名称
String tarPath = "";//图片最终保存的路径
MultipartFile file = null;
int FileLength = files.length;
if (files != null && FileLength > 0) {
//循环获取file数组中得文件
for (int i = 0; i < FileLength; i++) {
file = files[i];
imgName = file.getOriginalFilename();//上传的文件名称
InputStream input;
try {
input = file.getInputStream();
BufferedImage bufImg = ImageIO.read(input);
save( "path", bufImg, startX, startY , imgName, tarPath, width, height, tuUser.getUsername(), avatarMpper, reqParams);
jsonObj.put("code", 0);
jsonObj.put("msg", "获取头像成功");
} catch (Exception e) {
jsonObj.put("code", 1);
jsonObj.put("msg", "设置头像失败");;
LOG.e(e.toString());
}
}
}
return jsonObj;
}
**
* 保存截图。
* @param bufImg
* @param imgType
* @param tarPath
* @throws Exception
*/
public void save(String path,BufferedImage bufImg,int startX,int startY ,String imgName,String tarPath,int width,int height,String username,TuAccountAvatarMapper avatarMpper,Map<String
4000
, String> reqParams) throws Exception{
SftpFileOper sftpFileOper = new SftpFileOper();
InputStream ins = null;
FileMessage fileMessage = null;
String filename = "";
//截图
subImg = bufImg.getSubimage(startX, startY, width, height);
//保存截图
//压缩图片为指定尺寸
if(subImg.getWidth()!=width || subImg.getHeight()!=height){
BufferedImage tempImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
tempImg.getGraphics().drawImage(subImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0,null);
ins = bufferedImagToInputStream(tempImg);//
fileMessage = sftpFileOper.upSftpFileCommon(filename, ins, path);//完成数据提交上传到ftp服务器
}else{
fileMessage = sftpFileOper.upSftpFileCommon(filename, ins, path);//完成数据提交上传到ftp服务器
}
filename = fileMessage.getFileName();
addUserHeadData( avatarMpper, path, filename,username, reqParams);//向数据库插入数据
}
public InputStream bufferedImagToInputStream(BufferedImage tempImg) throws IOException{
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(tempImg, "gif", os);
InputStream inputStrem = new ByteArrayInputStream(os.toByteArray());
return inputStrem;
}
前台:1.imageSelect.jsp
<body>
<script type="text/javascript">
function preview(img, selection) {
if (!selection.width || !selection.height)
return;
var scaleX = 100 / selection.width;
var scaleY = 100 / selection.height;
$('#preview img').css({
width : Math.round(scaleX * 300),
height : Math.round(scaleY * 300),
marginLeft : -Math.round(scaleX * selection.x1),
marginTop : -Math.round(scaleY * selection.y1)
});
}
$(function() {
$('#photo').imgAreaSelect({
aspectRatio : '1:1',
handles : true,
fadeSpeed : 200,
onSelectChange : preview
});
});
</script>
<div class="frame" style="width: 300px; height: 300px;float:left;margin-right:50px">
<img id="photo" src="files/flower2.jpg">
</div>
<div id="preview" style="width: 100px; height: 100px; overflow: hidden;">
<img src="files/flower2.jpg" style="width: 100px; height: 100px;">
</div>
</body>
后台:2.imageTool.java
public class ImgTool {
private BufferedImage subImg;
/**
* 截图
* @param srcPath
* @param startX
* @param startY
* @param width
* @param height
*/
public void cut(String srcPath,int startX,int startY,int width,int height){
try {
BufferedImage bufImg = ImageIO.read(new File(srcPath));
subImg = bufImg.getSubimage(startX, startY, width, height);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 保存截图。
* @param bufImg
* @param imgType
* @param tarPath
*/
public void save(String imgType,String imgName,String tarPath,int width,int height){
try {/**压缩图片为指定尺寸*/
if(subImg.getWidth()!=width || subImg.getHeight()!=height){
BufferedImage tempImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
tempImg.getGraphics().drawImage(subImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0,null);
ImageIO.write(tempImg, imgType, new File(tarPath+"/"+imgName));
}else{
ImageIO.write(subImg,imgType,new File(tarPath+"/"+imgName));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在正是开始了。这是我在项目中的写法,希望能对大家有帮助:
1.先将 BufferedImage转换成InputStream,将文件接收到并裁减成自己想要的大小规格。
2.然后完成后 InputStream转换成 BufferedImage
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(tempImg, "gif", os);
InputStream inputStrem = new ByteArrayInputStream(os.toByteArray());
3,。最后上传到ftp文件服务器:
fileMessage = sftpFileOper.upSftpFileCommon(filename, ins, path);//完成数据提交上传到ftp服务器
public JSONObject setUserHead(Map<String, Object> urlParams,Map<String, String> reqParams,
TuUser tuUser,TuAccountAvatarMapper avatarMpper){
JSONObject jsonObj = new JSONObject();
//reqParams里面获取的参数:startX,startY, width, height, imgName, imgType
//:String srcPath,int startX,int startY,int width,int height
//获取源文件流:String srcPath;
//配置文件中读取ftp上的保存路径:String tarPath
Map<String , Object> map = new HashMap<>();
MultipartFile[] files = (MultipartFile[]) urlParams.get("file");
int startX = Integer.parseInt(reqParams.get("startX"));
int startY = Integer.parseInt(reqParams.get("startY"));
int width = Integer.parseInt(reqParams.get("width"));
int height = Integer.parseInt(reqParams.get("height"));
String imgName = "";//图片名称
String tarPath = "";//图片最终保存的路径
MultipartFile file = null;
int FileLength = files.length;
if (files != null && FileLength > 0) {
//循环获取file数组中得文件
for (int i = 0; i < FileLength; i++) {
file = files[i];
imgName = file.getOriginalFilename();//上传的文件名称
InputStream input;
try {
input = file.getInputStream();
BufferedImage bufImg = ImageIO.read(input);
save( "path", bufImg, startX, startY , imgName, tarPath, width, height, tuUser.getUsername(), avatarMpper, reqParams);
jsonObj.put("code", 0);
jsonObj.put("msg", "获取头像成功");
} catch (Exception e) {
jsonObj.put("code", 1);
jsonObj.put("msg", "设置头像失败");;
LOG.e(e.toString());
}
}
}
return jsonObj;
}
**
* 保存截图。
* @param bufImg
* @param imgType
* @param tarPath
* @throws Exception
*/
public void save(String path,BufferedImage bufImg,int startX,int startY ,String imgName,String tarPath,int width,int height,String username,TuAccountAvatarMapper avatarMpper,Map<String
4000
, String> reqParams) throws Exception{
SftpFileOper sftpFileOper = new SftpFileOper();
InputStream ins = null;
FileMessage fileMessage = null;
String filename = "";
//截图
subImg = bufImg.getSubimage(startX, startY, width, height);
//保存截图
//压缩图片为指定尺寸
if(subImg.getWidth()!=width || subImg.getHeight()!=height){
BufferedImage tempImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
tempImg.getGraphics().drawImage(subImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0,null);
ins = bufferedImagToInputStream(tempImg);//
fileMessage = sftpFileOper.upSftpFileCommon(filename, ins, path);//完成数据提交上传到ftp服务器
}else{
fileMessage = sftpFileOper.upSftpFileCommon(filename, ins, path);//完成数据提交上传到ftp服务器
}
filename = fileMessage.getFileName();
addUserHeadData( avatarMpper, path, filename,username, reqParams);//向数据库插入数据
}
public InputStream bufferedImagToInputStream(BufferedImage tempImg) throws IOException{
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(tempImg, "gif", os);
InputStream inputStrem = new ByteArrayInputStream(os.toByteArray());
return inputStrem;
}
相关文章推荐
- Java 使用imgAreaSelect插件进行图片上传切割
- “上传图片到服务器”之SWFUpload与imgAreaSelect的使用----实现切图效果
- imgAreaSelect做前台图片截取+java后台处理
- 利用jQuery插件imgAreaSelect实现图片上传裁剪(同步显示图像位置信息)
- 利用jQuery插件imgAreaSelect实现图片上传裁剪(放大缩小)
- jQuery-图片上传裁剪插件--imgAreaSelect(分析四) 上传服务器端
- swfupload+imgareaselect打造头像图片在线编辑无刷新上传
- jQuery-图片上传裁剪插件--imgAreaSelect(分析二) 同步显示图像位置信息
- jQuery-图片上传裁剪插件--imgAreaSelect(分析三) 如何获得选择域的图像信息
- jQuery-图片上传裁剪插件--imgAreaSelect(分析一) 放大缩小
- JQ插件之imgAreaSelect实现对图片的在线截图功能(java版)
- JQ插件之imgAreaSelect实现对图片的在线截图功能(java版)
- ASP 实例:头像上传。文件(图片)上传,头像截取(Jquery的imgAreaSelect插件来选取截取区域)
- 利用imgareaselect辅助后台实现图片上传裁剪
- Uploadify & jQuery.imgAreaSelect 插件实现图片上传裁剪
- SWFUpload.js方法图片的上传和imgAreaselect.js方法图片任意部位的截取
- imgareaselect+java实现图片裁剪保存
- jQuery 图片剪裁插件使用之 imgAreaSelect
- Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image获取图像像素点image.getRGB(i, lineIndex); 图片剪辑/AtiPlatf_cms/src/com/attilax/img/imgx.javacutImage图片处理titit 判断判断一张图片是否包含另一张小图片 atitit 图片去噪算法的原理与
- java BufferedImage 合成多张图片