java图片上传被旋转,在其他大牛那看到的java手机图片上传旋转问题的解决方法...

// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理

//Base64所用包:org.apache.commons.codec.binary.Base64

public static String encodeImgageToBase64(File imageFile) {

byte[] data = null;

// 读取图片字节数组

try {

InputStream in = new FileInputStream(imageFile);

data = new byte[in.available()];

in.read(data);

in.close();

} catch (IOException e) {

e.printStackTrace();

}

// 对字节数组Base64编码

return Base64.encodeBase64String(data);// 返回Base64编码过的字节数组字符串

}

判断是否旋转:

/**

*

* @功能:解析base64图片编码,判断是否有旋转

* @param imgStr base64图片编码

* @param format 图片格式

* @param fileName 图片名称

*/

public static Map stringToImage(String imgStr, String format, String fileName){

//解码base64图片

byte[] bts = Base64.decodeBase64(imgStr);

InputStream is = new ByteArrayInputStream(bts);

try {

//获取照片的Exif信息

Metadata metadata = JpegMetadataReader.readMetadata(is);

Directory exif = metadata.getFirstDirectoryOfType(ExifDirectoryBase.class);

/*

//输出从图片获取到的相关信息

Iterable ites = metadata.getDirectories();

for (Directory directory : ites) {

Iterator tags = directory.getTags().iterator();

while(tags.hasNext()){

Tag tag = (Tag) tags.next();

System.out.println(tag);

}

}

*/

//获取照片拍摄方向

String type = exif.getString(ExifDirectoryBase.TAG_ORIENTATION);

//图片正常情况下,type=1,测试正常图片旋转,请修改case 条件为 1

if(StringUtil.notBlank(type)){//判断拍摄方向是否为空

switch (Integer.parseInt(type)) {

case 3://要进行180度旋转

byte[] bytes = rotateImg(bts , 180.0 , format);

return uploadToAliYunOSS(fileName, bytes);

case 6://要进行90度旋转

byte[] bytes1 = rotateImg(bts , 90.0 , format);

return uploadToAliYunOSS(fileName, bytes1);

case 8://要进行-90度旋转

byte[] bytes2 = rotateImg(bts , -90.0 , format);

return uploadToAliYunOSS(fileName, bytes2);

default :

return uploadToAliYunOSSBybase64(fileName, bts);

}

}else{

return uploadToAliYunOSSBybase64(fileName, bts);

}

} catch (Exception e) {

e.printStackTrace();

}

return Tool.getError(1, "请求错误!", "");

}

矫正图片旋转

/**

*

* @功能:矫正图片旋转,返回byte[]

* @param input

* @param angle

* @param format

* @param fileName

* @throws IOException void

*/

public static byte[] rotateImg(byte[] bytes , double angle , String format) throws IOException{

InputStream input = new ByteArrayInputStream(bytes);

BufferedImage old_img = ImageIO.read(input);

int width = old_img.getWidth();

int height = old_img.getHeight();

double[][] newPositions = new double[4][];

newPositions[0] = calculatePosition(0, 0, angle);

newPositions[1] = calculatePosition(width, 0, angle);

newPositions[2] = calculatePosition(0, height, angle);

newPositions[3] = calculatePosition(width, height, angle);

double minX = Math.min(

Math.min(newPositions[0][0], newPositions[1][0]),

Math.min(newPositions[2][0], newPositions[3][0])

);

double maxX = Math.max(

Math.max(newPositions[0][0], newPositions[1][0]),

Math.max(newPositions[2][0], newPositions[3][0])

);

double minY = Math.min(

Math.min(newPositions[0][1], newPositions[1][1]),

Math.min(newPositions[2][1], newPositions[3][1])

);

double maxY = Math.max(

Math.max(newPositions[0][1], newPositions[1][1]),

Math.max(newPositions[2][1], newPositions[3][1])

);

int newWidth = (int)Math.round(maxX - minX);

int newHeight = (int)Math.round(maxY - minY);

BufferedImage new_img = new BufferedImageBuilder(newWidth, newHeight , BufferedImage.TYPE_INT_BGR).build();

Graphics2D g = new_img.createGraphics();

g.setRenderingHint(

RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BILINEAR

);

g.setRenderingHint(

RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON

);

double w = newWidth / 2.0;

double h = newHeight / 2.0;

g.rotate(Math.toRadians(angle), w, h);

int centerX = (int)Math.round((newWidth - width) / 2.0);

int centerY = (int)Math.round((newHeight - height) / 2.0);

g.drawImage(old_img, centerX, centerY, null);

g.dispose();

//新建流。

ByteArrayOutputStream baos = new ByteArrayOutputStream();

//利用ImageIO类的write方法,将BufferedImage以png图片的数据模式写入流。

ImageIO.write(new_img, format, baos);

return baos.toByteArray();

}

rotateImg() 中的calculatePosition()方法

private static double[] calculatePosition(double x, double y, double angle){

angle = Math.toRadians(angle);

double nx = (Math.cos(angle) * x) - (Math.sin(angle) * y);

double ny = (Math.sin(angle) * x) + (Math.cos(angle) * y);

return new double[] {nx, ny};

}

本文中的方法出处:http://my.oschina.net/u/2344340/blog/699435

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441677.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【qduoj】奇数阶幻方 (构造)

题干: C语言_魔方阵 描述 魔方阵是一个古老的智力问题,它要求在一个mm的矩阵中填入1~m2的数字(m为奇数),使得每一行、每一列、每条对角线的累加和都相等,如下为5阶魔方阵示例。 15 8 1 24 17…

【牛客161 - A】字符串(尺取法,桶标记法)

题干: 时间限制:C/C 1秒,其他语言2秒 空间限制:C/C 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 小N现在有一个字符串S。他把这这个字符串的所有子串都挑了出来。一个S的子串T是合法的,当且仅…

mysql innodb 全表锁,Mysql InnoDB行锁及表锁分享

一. 背景知识二. 步入正题:表锁和行锁1.1. 表锁 vs 行锁在 MySQL 中锁的种类有很多,但是最基本的还是表锁和行锁:表锁指的是对一整张表加锁,一般是 DDL 处理时使用,也可以自己在 SQL 中指定;而行锁指的是锁…

【牛客 - 185A】无序组数 (思维,数学,因子个数)

题干: 时间限制:C/C 1秒,其他语言2秒 空间限制:C/C 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 给出一个二元组(A,B) 求出无序二元组(a,b) 使得&#x…

php万能查询用预,PHP 与 mysql

一、php 的 sql 注入攻击1.1、什么是 sql 注入攻击用户提交一段数据库查询代码,根据返回的结果,获得某些他想得到的数据。比如 :查询某个管理员是否存在,一般程序员会这么写$sql "select * from user where nameluluyii and…

php 判断radio选中哪个,jquery如何判断单选按钮radio是否选中

jquery判断单选按钮radio是否选中的方法:1、加载页面的时候获取id,代码为【var fs$("#"id).val()】;2、点击按钮的时候获取id,代码为【var id $(this).attr("id")】。本教程操作环境:windows7系统…

【qduoj】【超级楼梯进阶版】

题干: 描述 N级阶梯,人可以一步走一级,也可以一步走两级,求人从阶梯底端走到顶端可以有多少种不同的走法。 输入 一个整数n,代表台阶的阶数。 输出 求人从阶梯底端走到顶端可以有多少种不同的走法,输出结…

matlab在光学实验中的应用,matlab在光学实验中的应用

matlab在光学实验中的应用 《MATLAB》课程论文MATLAB 在光学实验中的应用姓名:学号:专业:班级:指导老师:学院:完成日期:1MATLAB 在波动光学中的应用(姓名:郑苗苗 12012241736 2012 级…

【HDU - 6016】Count the Sheep (思维,类似二分图)

题干: Altough Skipping the class is happy, the new term still can drive luras anxious which is of course because of the tests! Luras became worried as she wanted to skip the class, as well as to attend the BestCoder and also to prepare for test…

如何生成时间序列matlab,求助:在MATLAB里如何输入时间序列中的时间

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼function[logRS,logERS,V]RSana(x,n,method,q)%Syntax:[logRS,logERS,V]RSana(x,n,method,q)%____________________________________________%% Performs R/Sanalysis on a time series.%% logRS is thelog(R/S).% logERS is theEx…

【CF#192 A】Funky Numbers (二分,查找,二元组)

题干: As you very well know, this years funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two…

matlab考试试题,matlab-考试试题-

matlab-考试试题- MATLAB 考试试题 (1) 产生一个1x10的随机矩阵,大小位于( -5 5),并且按照从大到小的顺序排列好!(注:要程序和运行结果的截屏)答案:a10*rand(1,10)-5;bsort(a, descend )1.请产生一个100*5 的矩阵&…

【HDU - 1702 】ACboy needs your help again! (栈和队列,水题模拟)

题干: ACboy was kidnapped!! he miss his mother very much and is very scare now.You cant image how dark the room he was put into is, so poor :(. As a smart ACMer, you want to get ACboy out of the monsters labyrinth.But when you arrive at the g…

java中jframe不存在怎么办,java – 设置JFrame背景,为什么这不起作用?

打印加载图像的宽度(如果为-1)则图像未正确加载.img Toolkit.getDefaultToolkit().createImage("red.png");System.out.println(img.getWidth(null)); // check what it prints值得阅读Loading Images Using getResource上的Java Tutorial您可以根据图像位置尝试任何…

后盾网经典原创视频教程php,《后盾网经典原创视频教程:PHP》139集

目录0_1 后盾网_IIS环境下PHP开发环境安装0 后盾网_PHP集成环境安装视频教程1 PHP视频教程 PHP基础(一)2 PHP视频教程 PHP基础(二)3 PHP视频教程 PHP基础(三)4 PHP视频教程 数据类型(一)5 PHP视频教程 数据类型(二)6 PHP视频教程 数据类型(三)7 PHP视频教程 类型转换 外部变量8…

【HDU - 1022】Train Problem I (栈模拟,水题,思维)

题干: As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a proble…

【HDU - 1216 】Assistance Required (模拟,类似素数打表,不是素数问题!)

题干: After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty d…

任意阶魔方阵matlab程序,【精品】任意阶魔方阵算法(c语言)

n阶幻方是由前n^2(n的2次方)个自然数组成的一个n阶方阵,其各行、各列及两条对角线所含的n个数的和相等。洛书就是最基本的33阶魔方阵,做出某种最恰当的决定,横竖都有3个格。 0的倒数 a-1可以对于 n 阶单位矩阵 e 以及同阶的方阵 a…

【HDU - 1302】The Snail (模拟,水题)

题干: A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each succe…

悟空php微信复制的东西在哪找,微信收藏的文件在哪?从哪里能看到?

现在的微信有很多的小功能,非常的方便实用,但是很多功能大家都不知道,今天,开淘网小编就来教教大家怎么使用微信的“我的收藏”功能。这个功能非常实用,而且收藏的源文件删除的话,我们从收藏里还是一样能用…