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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题的原因是由于不同的设备在拍照后保存图片的方向不同,导致在显示图片出现旋转的情况。为了解决这个问题,可以通过以下方法进行处理: 1. 使用 ExifInterface 类 可以通过 ExifInterface 类读取图片旋转角度信息,然后将图片进行相应的旋转。具体的代码如下: ```java ExifInterface exif = new ExifInterface(photoPath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); ``` 2. 使用系统相机 在调用系统相机拍照时,可以通过设置 Camera.Parameters 属性来控制图片旋转角度。具体的代码如下: ```java Camera.Parameters parameters = mCamera.getParameters(); Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; } else { result = (info.orientation - degrees + 360) % 360; } mCamera.setDisplayOrientation(result); parameters.setRotation(result); ``` 以上两种方法都可以解决Android部分手机拍照后获取的图片旋转问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值