-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
881c8a7
commit ed058ee
Showing
10 changed files
with
394 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
android/src/main/java/com/mrousavy/blurhash/BlurhashModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.mrousavy.blurhash | ||
|
||
import android.graphics.Bitmap | ||
import android.net.Uri | ||
import com.facebook.common.executors.CallerThreadExecutor | ||
import com.facebook.common.references.CloseableReference | ||
import com.facebook.datasource.DataSource | ||
import com.facebook.drawee.backends.pipeline.Fresco | ||
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber | ||
import com.facebook.imagepipeline.image.CloseableImage | ||
import com.facebook.imagepipeline.request.ImageRequestBuilder | ||
import com.facebook.react.bridge.LifecycleEventListener | ||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.module.annotations.ReactModule | ||
import kotlin.concurrent.thread | ||
|
||
@ReactModule(name = NativeBlurhashModuleSpec.NAME) | ||
class BlurhashModule(reactContext: ReactApplicationContext) : | ||
NativeBlurhashModuleSpec(reactContext), LifecycleEventListener { | ||
@ReactMethod | ||
override fun createBlurhashFromImage( | ||
imageUri: String, | ||
componentsX: Double, | ||
componentsY: Double, | ||
promise: Promise | ||
) { | ||
thread(true) { | ||
if (componentsX < 1 || componentsY < 1) { | ||
promise.reject( | ||
"INVALID_COMPONENTS", | ||
Exception("The componentX and componentY arguments must be greater than 0!")) | ||
return@thread | ||
} | ||
try { | ||
val formattedUri = imageUri.trim() | ||
|
||
val imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(formattedUri)).build() | ||
val imagePipeline = Fresco.getImagePipeline() | ||
val dataSource = imagePipeline.fetchDecodedImage(imageRequest, reactApplicationContext) | ||
dataSource.subscribe( | ||
object : BaseBitmapDataSubscriber() { | ||
override fun onNewResultImpl(bitmap: Bitmap?) { | ||
try { | ||
if (dataSource.isFinished && bitmap != null) { | ||
val blurhash = BlurHashEncoder.encode(bitmap, componentsX.toInt(), componentsY.toInt()) | ||
promise.resolve(blurhash) | ||
} else { | ||
if (dataSource.failureCause != null) { | ||
promise.reject("LOAD_ERROR", dataSource.failureCause) | ||
} else { | ||
promise.reject("LOAD_ERROR", Exception("Failed to load URI!")) | ||
} | ||
} | ||
} finally { | ||
dataSource.close() | ||
} | ||
} | ||
|
||
override fun onFailureImpl( | ||
dataSource: DataSource<CloseableReference<CloseableImage>> | ||
) { | ||
try { | ||
if (dataSource.failureCause != null) { | ||
promise.reject("LOAD_ERROR", dataSource.failureCause) | ||
} else { | ||
promise.reject("LOAD_ERROR", Exception("Failed to load URI!")) | ||
} | ||
} finally { | ||
dataSource.close() | ||
} | ||
} | ||
}, | ||
CallerThreadExecutor.getInstance()) | ||
} catch (e: Exception) { | ||
promise.reject("INTERNAL", e) | ||
} | ||
} | ||
} | ||
|
||
@ReactMethod | ||
override fun clearCosineCache() { | ||
BlurHashDecoder.clearCache() | ||
} | ||
|
||
override fun onHostResume() {} | ||
|
||
override fun onHostPause() {} | ||
|
||
override fun onHostDestroy() { | ||
BlurHashDecoder.clearCache() | ||
} | ||
} |
44 changes: 32 additions & 12 deletions
44
android/src/main/java/com/mrousavy/blurhash/BlurhashPackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,42 @@ | ||
package com.mrousavy.blurhash | ||
|
||
import com.facebook.react.ReactPackage | ||
import com.facebook.react.bridge.JavaScriptModule | ||
import com.facebook.react.TurboReactPackage | ||
import com.facebook.react.bridge.NativeModule | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.module.annotations.ReactModule | ||
import com.facebook.react.module.model.ReactModuleInfo | ||
import com.facebook.react.module.model.ReactModuleInfoProvider | ||
import com.facebook.react.turbomodule.core.interfaces.TurboModule | ||
import com.facebook.react.uimanager.ViewManager | ||
|
||
class BlurhashPackage : ReactPackage { | ||
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { | ||
return listOf<NativeModule>(BlurhashViewModule(reactContext)) | ||
class BlurhashPackage : TurboReactPackage() { | ||
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? { | ||
return when (name) { | ||
NativeBlurhashModuleSpec.NAME -> BlurhashModule(reactContext) | ||
else -> null | ||
} | ||
} | ||
|
||
// Created for legacy support, it's deprecated now. | ||
fun createJSModules(): List<Class<out JavaScriptModule?>> { | ||
return emptyList() | ||
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider { | ||
val moduleList: Array<Class<out NativeModule?>> = arrayOf(BlurhashModule::class.java) | ||
val reactModuleInfoMap: MutableMap<String, ReactModuleInfo> = HashMap() | ||
for (moduleClass in moduleList) { | ||
val reactModule = moduleClass.getAnnotation(ReactModule::class.java) ?: continue | ||
reactModuleInfoMap[reactModule.name] = | ||
ReactModuleInfo( | ||
reactModule.name, | ||
moduleClass.name, | ||
true, | ||
reactModule.needsEagerInit, | ||
/** TODO remove the parameter once support for RN < 0.73 is dropped */ | ||
reactModule.hasConstants, | ||
reactModule.isCxxModule, | ||
TurboModule::class.java.isAssignableFrom(moduleClass)) | ||
} | ||
return ReactModuleInfoProvider { reactModuleInfoMap } | ||
} | ||
|
||
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { | ||
return listOf<ViewManager<*, *>>(BlurhashViewManager()) | ||
} | ||
} | ||
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { | ||
return listOf<ViewManager<*, *>>(BlurhashViewManager()) | ||
} | ||
} |
Oops, something went wrong.