ProductLookup
Product details returned by a UPC lookup. Look up a UPC with RecognizerClient; see Looking up a UPC. Added in 2.5.0.
Package: com.microblink.core
Declaration
data class ProductLookup @JvmOverloads constructor(
var upc: String? = null,
var name: String? = null,
var url: String? = null,
var brand: String? = null,
var rewarded: Boolean = false,
var brandParticipates: Boolean = false,
var message: String? = null,
) : Parcelable
Quick Reference
Properties
| Property | Description |
|---|---|
upc | UPC of the product |
name | Product name |
url | URL of the product image |
brand | Brand name |
rewarded | Whether the product participates in your rewards program |
brandParticipates | Whether the product's brand participates in your rewards program |
message | Server message, or the error when the lookup failed |
Properties
upc
var upc: String?
// Java: getUpc() / setUpc(String)
UPC of the product.
name
var name: String?
// Java: getName() / setName(String)
Product name.
url
var url: String?
// Java: getUrl() / setUrl(String)
URL of the product image.
brand
var brand: String?
// Java: getBrand() / setBrand(String)
Brand name.
rewarded
var rewarded: Boolean
// Java: isRewarded() / setRewarded(boolean)
true if the product participates in your rewards program.
Default: false
brandParticipates
var brandParticipates: Boolean
// Java: isBrandParticipates() / setBrandParticipates(boolean)
true if the product's brand participates in your rewards program.
Default: false
message
var message: String?
// Java: getMessage() / setMessage(String)
Message returned by the server with the result. When the lookup fails because of a network or server error, it holds the error and the other properties keep their defaults.
Looking up a UPC
RecognizerClient is in com.microblink (blinkreceipt-recognizer).
class RecognizerClient() {
@JvmOverloads
fun lookup(
upc: String,
cancellationToken: CancellationToken? = null, // com.google.android.gms.tasks.CancellationToken
): Task<ProductLookup> // com.google.android.gms.tasks.Task
}
lookup() runs the network request on Dispatchers.IO. The returned task never fails:
- A network or server error, including a timeout, is reported in
message. - A UPC the server does not know returns an empty
ProductLookup(every property at its default). - Cancelling
cancellationTokenabandons the request and cancels the task. A token that is already cancelled returns a cancelled task without making a request.
Task and CancellationToken come from play-services-tasks, which blinkreceipt-recognizer exposes to your app. Kotlin callers can suspend on the task with kotlinx.coroutines.tasks.await(cancellation), which cancels the request when the coroutine is cancelled; add org.jetbrains.kotlinx:kotlinx-coroutines-play-services to your app for it.
- Kotlin
- Java
import com.google.android.gms.tasks.CancellationTokenSource
import com.microblink.RecognizerClient
import kotlinx.coroutines.tasks.await
// Suspends; call from a coroutine. Cancelling the coroutine cancels the request.
val cancellation = CancellationTokenSource()
val product = RecognizerClient().lookup("012345678905", cancellation.token).await(cancellation)
if (product.message == null) {
showProduct(product.name, product.brand, product.rewarded)
}
import com.google.android.gms.tasks.CancellationTokenSource;
import com.microblink.RecognizerClient;
RecognizerClient client = new RecognizerClient();
CancellationTokenSource cancellation = new CancellationTokenSource();
client.lookup("012345678905", cancellation.getToken()).addOnSuccessListener(product -> {
if (product.getMessage() == null) {
showProduct(product.getName(), product.getBrand(), product.isRewarded());
}
});
// Later, for example when the screen closes:
cancellation.cancel();
com.microblink.RecognizerClient replaces com.microblink.barcode.RecognizerClient, which was removed with the barcode module. The unrelated com.microblink.ProductLookup interface from earlier versions is no longer public; the name now belongs to this class in com.microblink.core.