- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
首先,我(想)做的事情:压缩和缩小一批图片 (jpg)。假设原始图片的尺寸为 1600w x 1200h。现在,我想要一个 1600x1200 的压缩副本以及另一个 800x600 和 400x300 的副本。
我用的是:我正在使用 libJpegTurob 来实现这一点。如果 LibJpegTurob 有问题,我会尝试使用 android 给定的方法。
已经尝试过:首先,我使用了从 Tom Gall (https://github.com/jberkel/libjpeg-turbo) 移植的 Java Wrapper。
在我开始使用超过 4mb 的图片之前,一切都很好(在 nexus 4 上)。基本上发生的是 android 抛出 OutOfMemory 异常。当我使用较小的图片 (~1-2mb) 但一张一张地压缩时,就会发生这种情况。
在 nexus s 等内存较低的廉价设备上运行后,情况变得更糟。低堆导致的问题,我是这么认为的。
那么,我想,我必须用 c 来做。内存问题似乎已解决,只要我在预算设备上使用小于 3mb 的图片。在 Nexus 4 上,我什至可以压缩一张 >15mb 的图片。
这是src图片。
但是现在……问题来了。第一张压缩图看起来不错
但是其他的都是这样的 或者这个
只要我继续选择图片并压缩它们,就会发生这种情况。
现在是代码。
这是缩放和压缩发生的地方
#include "_HelloJNI.h"
#include <errno.h>
#include <jni.h>
#include <sys/time.h>
#include <time.h>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <android/bitmap.h>
#include <unistd.h>
#include <setjmp.h>
#include "jpeglib.h"
#include "turbojpeg.h"
#define LOG_TAG "DEBUG"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE,LOG_TAG,__VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
int IMAGE_COMPRESS_QUALITY = 80;
typedef struct {
int width;
int height;
}tSize;
JNIEXPORT jint JNICALL Java_com_example_LibJpegTurboTest_NdkCall_nativeCompress
(JNIEnv * env, jobject onj, jstring jniSrcImgPath, jstring jniDestDir, jstring jniDestImgName, jint jniSrcWidth, jint jniSrcHeight) {
int pyramidRet = 0;
tSize fileSize;
fileSize.width = (int)jniSrcWidth;
fileSize.height = (int)jniSrcHeight;
const char* srcImgPath = (*env)->GetStringUTFChars(env, jniSrcImgPath, 0);
const char* destDir = (*env)->GetStringUTFChars(env, jniDestDir, 0);
const char* destFileName = (*env)->GetStringUTFChars(env, jniDestImgName, 0);
pyramidRet = createPreviewPyramidUsingCustomScaling(srcImgPath, destDir, destFileName, fileSize, 4);
return 0;
}
static tSize imageSizeForStep(int step, tSize *originalSize) {
float factor = 1 / pow(2, step);
return (tSize) {
round(originalSize->width * factor),
round(originalSize->height * factor) };
}
int saveBitmapBufferImage(unsigned char *data, tSize *imageSize, char *destFileName, int quality) {
int retValue = 1;
int res = 0;
unsigned long destinationJpegBufferSize = 0;
tjhandle tjCompressHandle = NULL;
unsigned char *destinationJpegBuffer = NULL;
FILE *file = NULL;
// jpgeg compress
tjCompressHandle = tjInitCompress();
if(tjCompressHandle == NULL) {
retValue = -1;
goto cleanup;
}
res = tjCompress2(tjCompressHandle, data, imageSize->width, imageSize->width * tjPixelSize[TJPF_RGBX], imageSize->height, TJPF_RGBX, &destinationJpegBuffer, &destinationJpegBufferSize, 1,
quality, TJFLAG_FASTUPSAMPLE);
if(res < 0) {
retValue = -1;
goto cleanup;
}
file = fopen(destFileName, "wb");
if(file == NULL) {
retValue = -1;
goto cleanup;
}
long written = fwrite(destinationJpegBuffer, destinationJpegBufferSize, 1, file);
retValue = (written == 1);
cleanup:
if(tjCompressHandle) {
tjDestroy(tjCompressHandle);
}
if(destinationJpegBuffer) {
tjFree(destinationJpegBuffer);
}
if(file) {
fclose(file);
}
return retValue;
}
int createBitmapBufferFromFile(char *srcFileName, tSize imageDimensions, long *bytesPerRow, long *dataBufferSize, unsigned char **dataBuffer) {
int retValue = 1;
int res = 0;
FILE *file = NULL;
unsigned char* sourceJpegBuffer = NULL;
long sourceJpegBufferSize = 0;
tjhandle tjDecompressHandle = NULL;
int fileWidth = 0, fileHeight = 0, jpegSubsamp = 0;
unsigned char* temp = NULL;
unsigned char* rotatedSourceJpegBuffer = NULL;
tjhandle tjTransformHandle = NULL;
file = fopen(srcFileName, "rb");
if (file == NULL) {
retValue = -1;
goto cleanup;
}
res = fseek(file, 0, SEEK_END);
if(res < 0) {
retValue = -1;
goto cleanup;
}
sourceJpegBufferSize = ftell(file);
if(sourceJpegBufferSize <= 0) {
retValue = -1;
goto cleanup;
}
sourceJpegBuffer = tjAlloc(sourceJpegBufferSize);
if(sourceJpegBuffer == NULL) {
retValue = -1;
goto cleanup;
}
res = fseek(file, 0, SEEK_SET);
if(res < 0) {
retValue = -1;
goto cleanup;
}
res = fread(sourceJpegBuffer, (long)sourceJpegBufferSize, 1, file);
if(res != 1) {
retValue = -1;
goto cleanup;
}
tjDecompressHandle = tjInitDecompress();
if(tjDecompressHandle == NULL) {
retValue = -1;
goto cleanup;
}
// decompress header to get image dimensions
res = tjDecompressHeader2(tjDecompressHandle, sourceJpegBuffer, sourceJpegBufferSize, &fileWidth, &fileHeight, &jpegSubsamp);
if(res < 0) {
retValue = -1;
goto cleanup;
}
float destWidth = (float)imageDimensions.width;
float destHeight = (float)imageDimensions.height;
*bytesPerRow = destWidth * tjPixelSize[TJPF_RGBX];
// buffer for uncompressed image-data
*dataBufferSize = *bytesPerRow * destHeight;
temp = tjAlloc(*dataBufferSize);
if(temp == NULL) {
retValue = -1;
goto cleanup;
}
res = tjDecompress2(tjDecompressHandle,
sourceJpegBuffer,
sourceJpegBufferSize,
temp,
destWidth,
*bytesPerRow,
destHeight,
TJPF_RGBX,
TJ_FASTUPSAMPLE);
if(res < 0) {
retValue = -1;
goto cleanup;
}
*dataBuffer = temp;
temp = NULL;
cleanup:
if(file) {
fclose(file);
}
if(sourceJpegBuffer) {
tjFree(sourceJpegBuffer);
}
if(tjDecompressHandle) {
tjDestroy(tjDecompressHandle);
}
if(temp) {
tjFree(temp);
}
return retValue;
}
int createPreviewPyramidUsingCustomScaling(char* srcImgPath, char* destDir, char* destFileName, tSize orginalImgSize, int maxStep) {
int retValue = 1;
int res = 1;
int success = 0;
int loopStep = 0;
tSize previewSize;
long bytesPerRow;
long oldBytesPerRow = 0;
unsigned char* sourceDataBuffer = NULL;
long sourceDataBufferSize = 0;
unsigned char* destinationDataBuffer = NULL;
long destinationDataBufferSize = 0;
unsigned char* buf1 = NULL;
unsigned char* buf2 = NULL;
long workBufSize = 0;
void* sourceRow = NULL;
void* targetRow = NULL;
char* destFilePrefix = "sample_";
char* fooDestName;
char* fooStrBuilder;
tSize orginSizeTmp;
orginSizeTmp.width = orginalImgSize.width;
orginSizeTmp.height = orginalImgSize.height;
previewSize = imageSizeForStep(1, &orginSizeTmp);
long width = (long)previewSize.width;
long height = (long)previewSize.height;
int errorCode = 0;
errorCode = createBitmapBufferFromFile(srcImgPath, previewSize, &bytesPerRow, &sourceDataBufferSize, &buf1);
if(errorCode != 1) {
retValue = errorCode;
goto cleanup;
}
workBufSize = sourceDataBufferSize;
buf2 = tjAlloc(workBufSize);
if(buf2 == NULL) {
retValue = -1;
goto cleanup;
} else {
memset(buf2,0,workBufSize);
}
sourceDataBuffer = buf1;
fooDestName = strcat(destDir, destFilePrefix);
fooStrBuilder = strcat(fooDestName, "1_");
fooDestName = strcat(fooStrBuilder, destFileName);
success = saveBitmapBufferImage(sourceDataBuffer, &previewSize, fooDestName, IMAGE_COMPRESS_QUALITY);
if(success <= 0) {
retValue = -1;
goto cleanup;
}
cleanup:
if(sourceDataBuffer) {
tjFree(sourceDataBuffer);
}
if(destinationDataBuffer) {
tjFree(destinationDataBuffer);
}
return retValue;
}
开始压缩的 Java 部分..
private void invokeCompress(ArrayList<PictureItem> picturesToCompress) {
if(picturesToCompress != null && picturesToCompress.size() > 0) {
for(int i=0; i<picturesToCompress.size(); i++) {
String srcPicturePath = picturesToCompress.get(i).getSrcImg();
String destDir = "/storage/emulated/0/1_TEST_FOLDER/";
String destFileName = getRandomString(4)+".jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(srcPicturePath, options);
try {
ndkCall.compress(srcPicturePath, destDir, destFileName, options.outWidth, options.outHeight);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
我做错了什么???
非常感谢!!
附言对不起英语不好!
最佳答案
我觉得不错。您确定 libjpegturbo 源有效且稳定吗?
关于android - 使用 libjpegturbo 压缩一批图片时出现奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18377807/
我有点想做 the reverse of this. 我不想解压缩并将收集文件添加到 S3 应用户要求: 生成一堆xml文件 使用一些图像(托管在 s3 上的预先存在的图像)压缩 xml 文件 下载
将此添加到域的虚拟主机后 AddOutputFilterByType DEFLATE application/javascript text/javascript text/css 响应头不包含任何内
在 Apache Im 中,通过将以下内容添加到我的 .htaccess 文件来启用输出压缩: # compress text, html, javascript, css, xml: AddOutp
是否可以以压缩格式将请求数据从浏览器发送到服务器? 如果是,我们该怎么做? 最佳答案 压缩从浏览器发送到服务器的数据是不受 native 支持 在浏览器中。 您必须找到一种解决方法,使用客户端语言(可
我正在寻找可以压缩JavaScript源代码的工具。我发现一些网络工具只能删除空格字符?但也许存在更好的工具,可以压缩用户的函数名称、字段名称、删除未使用的字段等。 最佳答案 经常用来压缩JS代码的工
使用赛马博彩场景,假设我有许多单独的投注来预测比赛的前 4 名选手 (superfecta)。 赌注如下... 1/2/3/4 1/2/3/5 1/2/4/3 1/2/4/5 1/2/5/3
我是一名实习生,被要求对 SQL 2008 数据压缩进行一些研究。我们想将 Outlook 电子邮件的几个部分存储在一个表中。问题是我们想将整个电子邮件正文存储在一个字段中,然后又想压缩它。使用 Ch
我目前有一个系统,用户可以在其中上传 MP4 文件,并且可以在移动设备上下载该文件。但有时,这些视频的大小超过 5MB,在我国,大多数人使用 2G。因此,下载大型视频通常需要 15-20 分钟。 有什
假设我有一个带有类型列的简单文档表: Documents Id Type 1 A 2 A 3 B 4 C 5 C 6 A 7 A 8 A 9 B 10 C 用户
我有一个较大字符串中的(子)字符串位置的 data.frame。数据包含(子)字符串的开头及其长度。可以很容易地计算出(子)字符串的结束位置。 data1 start length end #>
我想知道是否 文件加密算法可以设计成它也可以执行文件压缩的事件(任何活生生的例子?)。 我也可以将它集成到移动短信服务中,我的意思是短信吗? 另外我想知道二进制文件...如果纯文本文件以二进制编码
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我们有几个具有大量 JavaScript 的 Java 项目,目前我们使用的是旧版本的 YUICompressor (2.4.2)。然而,我在这篇博文中发现 YUICompressor 正在 depr
从之前关于尝试提高网站性能的文章中,我一直在研究 HTTP 压缩。我读过有关在 IIS 中设置它的信息,但它似乎是所有 IIS 应用程序池的全局事物,我可能不允许这样做,因为还有另一个站点在其上运行。
我有一个 REST 服务,它返回一大块 XML,大约值(value) 150k。 例如http://xmlservice.com/services/RestService.svc/GetLargeXM
我正在尝试获取一个简单的 UglifyJS (v2.3.6) 示例来处理压缩。 具体来说,“未使用”选项,如果从未使用过,变量和函数将被删除。 这是我在命令行上的尝试: echo "function
我正在开发一个项目,如果我的磁盘出现问题,我将在使用 ZLIB 压缩内存块后将其发送到另一个磁盘。然后我计划下载该转储并用于进一步调试。这种压缩和上传将一次完成一个 block - 比如说 1024
LZW 压缩算法在压缩后增加了位大小: 这是压缩函数的代码: // compression void compress(FILE *inputFile, FILE *outputFile) {
我的问题与如何在 3D 地形上存储大量信息有关。这些信息应该是 secret 的,因为它们非常庞大,也应该被压缩。我选择了文件存储,现在我想知道将对象数据加密/压缩(或压缩/加密)到文件的最佳做法。
我使用以下代码来压缩我的文件并且效果很好,但我只想压缩子文件夹而不是在压缩文件中显示树的根。 public boolean zipFileAtPath(String sourcePath, Strin
我是一名优秀的程序员,十分优秀!