java 图片压缩 base64,图片的尺寸 大小压缩 和转化为base64

//转化为jpg图片并压缩方法:

UIImageJPEGRepresentation(image, compression);

/**

压图片质量

@param image image

*/

- (NSData *)zipImageWithImage:(UIImage *)image

{

if (!image) {

return nil;

}

CGFloat maxFileSize = 500*1024;

CGFloat compression = 0.9f;

NSData *compressedData = UIImageJPEGRepresentation(image, compression);

while ([compressedData length] > maxFileSize) {

compression *= 0.9;

compressedData = UIImageJPEGRepresentation([self compressImage:image newWidth:image.size.width*compression], compression);

}

return compressedData;

}

/**

*  等比缩放本图片大小

*  @param newImageWidth 缩放后图片宽度,像素为单位

*  @return self-->(image)

*/

- (UIImage *)compressImage:(UIImage *)image newWidth:(CGFloat)newImageWidth

{

if (!image) return nil;

float imageWidth = image.size.width;

float imageHeight = image.size.height;

float width = newImageWidth;

float height = image.size.height/(image.size.width/width);

float widthScale = imageWidth /width;

float heightScale = imageHeight /height;

// 创建一个bitmap的context

// 并把它设置成为当前正在使用的context

UIGraphicsBeginImageContext(CGSizeMake(width, height));

if (widthScale > heightScale) {

[image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];

}

else {

[image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];

}

// 从当前context中创建一个改变大小后的图片

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// 使当前的context出堆栈

UIGraphicsEndImageContext();

return newImage;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的`java.util.zip`和`java.util.Base64`类来压缩和编码Base64图片。 下面是一个示例代码,展示了如何压缩Base64图片: ```java import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Base64; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; public class ImageCompression { public static String compressImageToBase64(String base64Image) throws IOException { // 将Base64字符串解码为字节数组 byte[] imageBytes = Base64.getDecoder().decode(base64Image); // 创建一个新的字节数组输出流 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // 创建一个压缩输出流,并将其连接到字节数组输出流 DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(outputStream, new Deflater()); // 将图像字节数组写入压缩输出流 deflaterOutputStream.write(imageBytes); deflaterOutputStream.close(); // 获取压缩后的图像字节数组 byte[] compressedImageBytes = outputStream.toByteArray(); // 将压缩后的图像字节数组编码为Base64字符串 String compressedBase64Image = Base64.getEncoder().encodeToString(compressedImageBytes); return compressedBase64Image; } } ``` 使用示例: ```java String base64Image = "your_base64_image_here"; try { String compressedBase64Image = ImageCompression.compressImageToBase64(base64Image); System.out.println("Compressed Base64 Image: " + compressedBase64Image); } catch (IOException e) { e.printStackTrace(); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值