static bool Bitmap_compress(JNIEnv* env, jobject clazz, SkBitmap* bitmap, int format, int quality, jobject jstream, jbyteArray jstorage) { SkImageEncoder::Type fm; switch (format) { case kJPEG_JavaEncodeFormat: fm = SkImageEncoder::kJPEG_Type; break; case kPNG_JavaEncodeFormat: fm = SkImageEncoder::kPNG_Type; break; case kWEBP_JavaEncodeFormat: fm = SkImageEncoder::kWEBP_Type; break; default: return false; } bool success = false; SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); if (NULL != strm) { SkImageEncoder* encoder = SkImageEncoder::Create(fm); if (NULL != encoder) { success = encoder->encodeStream(strm, *bitmap, quality); delete encoder; } delete strm; } return success; }
/****************************************************************************** * convert rgb565 to jpeg file *******************************************************************************/ bool rgb565toJpeg(uint8_t *pbufIn, uint8_t *pbufOut, uint32_t width, uint32_t height, uint32_t *psize) { uint32_t size = 0; // It's sw encode, the buffer address must be virtual address CAM_LOGD("[rgb565toJpeg] pbufIn: 0x%x, pbufOut: 0x%x, width: %d, height: %d \n", (uint32_t) pbufIn, (uint32_t) pbufOut, width, height); SkImageEncoder::Type fm = SkImageEncoder::kJPEG_Type; bool success = false; SkImageEncoder *pencoder = SkImageEncoder::Create(fm); if (NULL != pencoder) { SkMemoryWStream *pskMemStream = new SkMemoryWStream(pbufOut, width * height * 2); if (NULL != pskMemStream) { SkBitmap *pbitmap = new SkBitmap; if (NULL != pbitmap) { pbitmap->setConfig(SkBitmap::kRGB_565_Config, width, height); pbitmap->setPixels(pbufIn); success = pencoder->encodeStream(pskMemStream, *pbitmap, 85); if (success) { size = pskMemStream->getBytesWritten(); } } delete pbitmap; } delete pskMemStream; delete pencoder; } *psize = size; CAM_LOGD(" size: %d \n", size); return success; }
static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle, jint format, jint quality, jobject jstream, jbyteArray jstorage) { SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); SkImageEncoder::Type fm; switch (format) { case kJPEG_JavaEncodeFormat: fm = SkImageEncoder::kJPEG_Type; break; case kPNG_JavaEncodeFormat: fm = SkImageEncoder::kPNG_Type; break; case kWEBP_JavaEncodeFormat: fm = SkImageEncoder::kWEBP_Type; break; default: return JNI_FALSE; } bool success = false; if (NULL != bitmap) { SkAutoLockPixels alp(*bitmap); if (NULL == bitmap->getPixels()) { return JNI_FALSE; } SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); if (NULL == strm) { return JNI_FALSE; } SkImageEncoder* encoder = SkImageEncoder::Create(fm); if (NULL != encoder) { success = encoder->encodeStream(strm, *bitmap, quality); delete encoder; } delete strm; } return success ? JNI_TRUE : JNI_FALSE; }