腾讯云文字识别图片压缩示例_AI解决方案_同尘科技

文字识别 2年前 (2023-06-29) 浏览 52

iOS 的压缩示例代码:

@implementation ImageCompressTool
/** 下面主要介绍了两种方式,具体使用的时候可以考虑两种方式合并使用 **/
/** * 质量压缩图片 * @param image 原始图片 * @param quality 质量压缩的目标 0~1.0f * @return */+ (UIImage *)doCompressQuality:(UIImage *)image quality:(CGFloat)quality { NSData * data = UIImageJPEGRepresentation(image, quality); return [UIImage imageWithData:data];}
/** * 指定大小缩放一张图片 * @param inputImage * @param newWith 新的尺寸宽度 * @param newHeight 新的尺寸高度 * @return */+ (UIImage *)scaleSizeImage:(UIImage *)inputImage newWith:(CGFloat)newWith newHeight:(CGFloat)newHeight { UIGraphicsBeginImageContext(CGSizeMake(newWith, newHeight)); [inputImage drawInRect:CGRectMake(0.0f, 0.0f, newWith, newHeight)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 如果要结合上面的方法,这里可以再做质量压缩 return newImage;}
@end

Android 的压缩示例代码:

/** * 图片压缩的帮助类型,可以参考的工具类 * * @author jerrydong * @since 2023/5/23 */public class ImageCompressUtils {
private static final String TAG = "ImageCompressUtils";
// Jpg的压缩比,可以根据需求修改 private static final int QUALITY_DEFAULT = 85;
/** * 主动压缩YUV图片的算法 * * @param image 待压缩的图片 * @return */ public static byte[] doCompressYuvImage(YuvImage image) { int dataWidth = image.getWidth(); int dataHeight = image.getHeight(); try (ByteArrayOutputStream outputSteam = new ByteArrayOutputStream()) { image.compressToJpeg(new Rect(0, 0, dataWidth, dataHeight), QUALITY_DEFAULT, outputSteam); // 转BitMap并压缩 Options options = new BitmapFactory.Options(); // 不使用透明通道 options.inPreferredConfig = Config.RGB_565; // 修改图片的采样率 options.inSampleSize = 2; Bitmap bmp = BitmapFactory.decodeByteArray(outputSteam.toByteArray(), 0, outputSteam.size(), options); outputSteam.reset(); // 主动压缩图片 bmp.compress(CompressFormat.JPEG, QUALITY_DEFAULT, outputSteam); return outputSteam.toByteArray(); } catch (IOException e) { Log.e(TAG, "Compress error"); } return null; }
/** * 主动压缩BitMap * * @param inputMap * @return */ public Bitmap doCompressBitMap(Bitmap inputMap) { try (ByteArrayOutputStream outputSteam = new ByteArrayOutputStream()) { inputMap.compress(CompressFormat.JPEG, QUALITY_DEFAULT, outputSteam); // 转BitMap并压缩(可以写通用方法!) Options options = new BitmapFactory.Options(); // 不使用透明通道 options.inPreferredConfig = Config.RGB_565; // 修改图片的采样率(可以根据实际情况计算) options.inSampleSize = 2; Bitmap bmp = BitmapFactory.decodeByteArray(outputSteam.toByteArray(), 0, outputSteam.size(), options); outputSteam.reset(); return bmp; } catch (IOException e) { Log.e(TAG, "Compress error"); } return null; }

/** * 缩放方法进行缩放 * * @param inputMap * @param scale 缩放大小 * @return */ public Bitmap matrixBitmap(Bitmap inputMap, float scale) { // 根据旋转角度,生成旋转矩阵 Matrix matrix = new Matrix(); matrix.setScale(scale, scale); Bitmap bitmap = Bitmap.createBitmap(inputMap, 0, 0, inputMap.getWidth(), inputMap.getHeight(), matrix, true); return bitmap; }
}

对解决方案有疑惑?想了解解决方案收费? 联系解决方案专家

腾讯云限时活动1折起,即将结束: 马上收藏

同尘科技为腾讯云授权服务中心,购买腾讯云享受折上折,更有现金返利:同意关联,立享优惠

阿里云解决方案也看看?: 点击对比阿里云的解决方案

- 0人点赞 -

发表点评 (0条)

not found

暂无评论,你要说点什么吗?