Skip to content

Commit

Permalink
Merge pull request #75 from iMasanari/develop
Browse files Browse the repository at this point in the history
dhashをライブラリ化
  • Loading branch information
iMasanari committed Apr 17, 2023
2 parents e148d06 + 6076e38 commit 999d084
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 81 deletions.
56 changes: 56 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
"action-reducer": "^0.4.0",
"any-base": "^1.1.0",
"i18next": "^22.4.14",
"isomorphic-image-hash": "^0.1.0",
"jspdf": "^2.5.1",
"promise-worker": "^2.0.1",
"react": "^18.2.0",
Expand Down
1 change: 0 additions & 1 deletion src/app/features/maybe/data/hash-list.json

This file was deleted.

4 changes: 3 additions & 1 deletion src/app/features/maybe/items/index.ts
Expand Up @@ -14,7 +14,9 @@ const ps = typeof window === 'object'
export const getItem = async (option: Option) => {
const cardHash = await ps.postMessage<string, Option>(option)

const res = await fetch(`${import.meta.env.VITE_HASH_API_URL}v1/${cardHash.slice(0, 2).toLowerCase()}.json`, {
const url = `${import.meta.env.VITE_HASH_API_URL}v2/cards/${cardHash.slice(0, 2).toLowerCase()}.json`

const res = await fetch(url, {
mode: 'cors',
})

Expand Down
25 changes: 19 additions & 6 deletions src/app/features/maybe/items/worker.ts
@@ -1,17 +1,30 @@
import { distance, dhashFromImageData, ImageDataLike } from 'isomorphic-image-hash'
import registerPromiseWorker from 'promise-worker/register'
import hashList from '../data/hash-list.json'
import { toBin } from '../utils/baseConverter'
import { distance, getHash } from '../utils/dhash'
import { ImagePixelData } from '../utils/resize'

export interface Option {
image: ImagePixelData
image: ImageDataLike
}

const hashDataList = hashList.map(hash => ({ hash, bin: toBin(hash) }))
let getHashList = async () => {
const fn = async () => {
const res = await fetch(`${import.meta.env.VITE_HASH_API_URL}v2/hash-list.json`)
const json: string[] = await res.json()

return json.map(hash => ({ hash, bin: toBin(hash) }))
}

const promise = fn()

// TODO: リトライ処理を追加する
getHashList = () => promise

return await promise
}

registerPromiseWorker<Option, string>(async (option) => {
const hash = getHash(option.image)
const hash = dhashFromImageData(option.image)
const hashDataList = await getHashList()

const result = hashDataList.reduce((acc, v) => {
const compare = distance(v.bin, hash)
Expand Down
13 changes: 0 additions & 13 deletions src/app/features/maybe/utils/color.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/app/features/maybe/utils/dhash.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/app/features/maybe/utils/resize.ts

This file was deleted.

0 comments on commit 999d084

Please sign in to comment.