app opens, press camera button
choose camera
snap a picture
picture is stored temporarily
take another picture
//hooks/usePhotoGallery.tsx
import { useState, useEffect } from "react";
import { useCamera } from '@ionic/react-hooks/camera';
import { useFilesystem, base64FromPath } from '@ionic/react-hooks/filesystem';
import { useStorage } from '@ionic/react-hooks/storage';
import { isPlatform } from '@ionic/react';
import {
CameraResultType, CameraSource, CameraPhoto,
Capacitor, FilesystemDirectory
} from "@capacitor/core";
export interface Photo {
filepath: string;
webviewPath?: string;
base64?: string;
}
export function usePhotoGallery() {
const [photos, setPhotos] = useState<Photo[]>([]);
const { getPhoto } = useCamera();
const takePhoto = async () => {
const cameraPhoto = await getPhoto({
resultType: CameraResultType.Uri,
source: CameraSource.Camera,
quality: 100
});
const fileName = new Date().getTime() + '.jpeg';
const newPhotos = [{
filepath: fileName,
webviewPath: cameraPhoto.webPath
}, ...photos];
setPhotos(newPhotos)
};
return {
photos,
takePhoto
};
}
-------------------------------------
//pages/tab2.tsx
import React from 'react';
import {
IonContent, IonHeader, IonPage, IonTitle, IonToolbar, IonButton,
IonGrid, IonRow, IonCol, IonImg, IonFab, IonFabButton, IonIcon
} from '@ionic/react';
import ExploreContainer from '../components/ExploreContainer';
import './Tab2.css';
import { usePhotoGallery } from '../hooks/usePhotoGallery';
import { camera } from 'ionicons/icons';
const Tab2: React.FC = () => {
const { photos, takePhoto } = usePhotoGallery();
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Camera Example</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonFab vertical='bottom' horizontal='center' slot='fixed'>
<IonFabButton onClick={() => takePhoto()}>
<IonIcon icon={camera} />
</IonFabButton>
</IonFab>
<IonGrid>
<IonRow>
{photos.map((photo, index) => (
<IonCol size="6" key={index}>
<IonImg src={photo.webviewPath} />
</IonCol>
))}
</IonRow>
</IonGrid>
</IonContent>
</IonPage>
);
};
export default Tab2;
reference:
taking photos
https://ionicframework.com/docs/react/your-first-app/2-taking-photos
ionic react-hooks
https://github.com/ionic-team/ionic-react-hooks
fab
http://chuanshuoge2.blogspot.com/2020/04/ionic-react-fab.html
http://chuanshuoge2.blogspot.com/2020/04/ionic-react-fab-2.html
No comments:
Post a Comment