Miniapp APIs
Media APIs
Use Nebula media APIs for scanning, choosing media, previewing images, and compressing assets.
Nebula provides a set of media-related Host APIs covering scanning, media selection, preview, compression, and saving. All media APIs are imported from @nebula-rn/client.
Scanning
scanCode
Open the host-managed scanning UI to scan QR codes or barcodes. If the user cancels, the API throws an error.
import { scanCode } from '@nebula-rn/client';
try {
const result = await scanCode();
console.log('Scan result:', result.result);
} catch (error) {
console.log('User cancelled scanning');
}scanCodeSafe
A safe version that does not throw, and instead returns an object with an ok state:
import { scanCodeSafe } from '@nebula-rn/client';
const result = await scanCodeSafe();
if (result.ok) {
console.log('Scan result:', result.data.result);
} else {
console.log('Scan failed or was cancelled:', result.error);
}Media selection
chooseMedia
Open the system photo library or camera so the user can choose images or videos:
import { chooseMedia } from '@nebula-rn/client';
const result = await chooseMedia();Image information
getImageInfo
Get metadata such as image width and height:
import { getImageInfo } from '@nebula-rn/client';
const info = await getImageInfo(imagePath);
console.log(`${info.width} x ${info.height}`);Image compression
compressImage
Compress or resize an image:
import { compressImage } from '@nebula-rn/client';
const result = await compressImage(imagePath, 80);
console.log('Compressed path:', result.tempFilePath);Image preview
previewImage
Open a full-screen image preview with swipe support:
import { previewImage } from '@nebula-rn/client';
await previewImage([
'https://example.com/photo1.jpg',
'https://example.com/photo2.jpg',
]);Save to the photo library
saveMedia
Save an image or video into the system library:
import { saveMedia } from '@nebula-rn/client';
await saveMedia(filePath, 'image');Screenshot listener
onUserCaptureScreen
Listen for user screenshot events:
import { onUserCaptureScreen } from '@nebula-rn/client';
const unsubscribe = onUserCaptureScreen(() => {
console.log('User captured the screen');
});
unsubscribe();Complete example
import React, { useState } from 'react';
import { Button, ScrollView, StyleSheet, Text } from 'react-native';
import { scanCode, chooseMedia } from '@nebula-rn/client';
export default function MediaDemoPage() {
const [scanResult, setScanResult] = useState('');
const handleScan = async () => {
try {
const result = await scanCode();
setScanResult(result.result);
} catch {
setScanResult('Cancelled');
}
};
const handleChoosePhoto = async () => {
await chooseMedia();
};
return (
<ScrollView contentContainerStyle={styles.container}>
<Button title="Scan code" onPress={handleScan} />
<Text>Scan result: {scanResult}</Text>
<Button title="Choose media" onPress={handleChoosePhoto} />
</ScrollView>
);
}
const styles = StyleSheet.create({
container: { padding: 20 },
});