在微信公众号里面需要上传头像,时间比较紧,调用学习jssdk并使用 来不及 就用了input
1、使用input:file标签, 去调用系统默认相机,摄像,录音功能,其实是有个capture属性,直接说明需要调用什么功能
= 10 && length / 1024 < 100) {
if (compress(in, saveFile, 10)) {
System.out.println("图片压缩十倍!");
}
} else if (length / 1024 >= 100 && length / 1024 < 1000) {
if (compress(in, saveFile, 100)) {
System.out.println("图片压缩100倍!");
}
} else if (length / 1024 >= 1000 && length / 1024 < 10000) {
if (compress(in, saveFile, 1000)) {
System.out.println("图片压缩1000倍!");
}
} else if (length / 1024 < 10 && length / 1024 > 0) {
if (compress(in, saveFile, 1)) {
System.out.println("图片压缩1倍!");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
String filename = "/Path/" + s + s + "." + suffix;//服务器地址
System.out.println(filename);
int a = shopService.updateImg(u.getId(), filename);
System.out.println(filename);
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
return "wode.html";
}
图片处理类
package com.example.springbootshop.util;
import org.junit.Test;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class ImageHelper {
private static BufferedImage resize(BufferedImage source, int targetW,
int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
// 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
// 则将下面的if else语句注释即可
if (sx < sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { // handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
// smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
public static boolean compress(String inFilePath, String outFilePath,
int width, int hight) {
boolean ret = false;
File file = new File(inFilePath);
File saveFile = new File(outFilePath);
InputStream in = null;
try {
in = new FileInputStream(file);
ret = compress(in, saveFile, width, hight);
} catch (FileNotFoundException e) {
e.printStackTrace();
ret = false;
} finally{
if(null != in){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ret;
}
public static boolean compress(InputStream in, File saveFile,
int width, int hight) {
// boolean ret = false;
BufferedImage srcImage = null;
try {
srcImage = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
return false;
}
if (width > 0 || hight > 0) {
// 原图的大小
int sw = srcImage.getWidth();
int sh = srcImage.getHeight();
// 如果原图像的大小小于要缩放的图像大小,直接将要缩放的图像复制过去
if (sw > width && sh > hight) {
srcImage = resize(srcImage, width, hight);
} else {
String fileName = saveFile.getName();
String formatName = fileName.substring(fileName
.lastIndexOf('.') + 1);
try {
ImageIO.write(srcImage, formatName, saveFile);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}
// 缩放后的图像的宽和高
int w = srcImage.getWidth();
int h = srcImage.getHeight();
// 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取
if (w == width) {
// 计算X轴坐标
int x = 0;
int y = h / 2 - hight / 2;
try {
saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
// 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取
else if (h == hight) {
// 计算X轴坐标
int x = w / 2 - width / 2;
int y = 0;
try {
saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
public static boolean compress(InputStream in, File saveFile, int proportion) {
if(null == in
||null == saveFile
||proportion < 1){// 检查参数有效性
//LoggerUtil.error(ImageHelper.class, "--invalid parameter, do nothing!");
return false;
}
BufferedImage srcImage = null;
try {
srcImage = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
return false;
}
// 原图的大小
int width = srcImage.getWidth() / proportion;
int hight = srcImage.getHeight() / proportion;
srcImage = resize(srcImage, width, hight);
// 缩放后的图像的宽和高
int w = srcImage.getWidth();
int h = srcImage.getHeight();
// 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取
if (w == width) {
// 计算X轴坐标
int x = 0;
int y = h / 2 - hight / 2;
try {
saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
// 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取
else if (h == hight) {
// 计算X轴坐标
int x = w / 2 - width / 2;
int y = 0;
try {
saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
private static void saveSubImage(BufferedImage image,
Rectangle subImageBounds, File subImageFile) throws IOException {
if (subImageBounds.x < 0 || subImageBounds.y < 0
|| subImageBounds.width - subImageBounds.x > image.getWidth()
|| subImageBounds.height - subImageBounds.y > image.getHeight()) {
//LoggerUtil.error(ImageHelper.class, "Bad subimage bounds");
return;
}
BufferedImage subImage = image.getSubimage(subImageBounds.x,subImageBounds.y, subImageBounds.width, subImageBounds.height);
String fileName = subImageFile.getName();
String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
ImageIO.write(subImage, formatName, subImageFile);
}
@Test
public static void main(String[] args) throws Exception {
//需要截图的长方形坐标
InputStream in = null;
//缩放后需要保存的路径
File saveFile = new File("D:\xiangmu\demo\20180523192742IMG_0049123.jpg");
try {
//原图片的路径
in = new FileInputStream(new File("D:\xiangmu\demo\20180523192742IMG_0049.jpg"));
if(compress(in, saveFile, 10)){
System.out.println("图片压缩十倍!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
}
以上这篇java input 调用手机相机和本地照片上传图片到服务器然后压缩的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



