Camera API の使用

警告 :

すべてのモバイル機能とモバイル関連機能は ColdFusion(2025リリース)で削除されました。

詳しくは、ColdFusion の非推奨の機能を参照してください。

開始する前に、カメラ関数を参照してください。

カメラの起動と画像のキャプチャ

<cfclientsettings enableDeviceAPI=true>

<cfclient>

<cffunction access=&quot;public&quot; name=&quot;getPicCam&quot; returntype=&quot;void&quot; >
<cfset opt = cfclient.camera.getOptions()>
<cfset res = cfclient.camera.getPicture(opt,false)>
<cfset imgStr=cfclient.file.readAsBase64(res)>
<cfset document.getElementById('myimg').src=imgStr>
<cfset document.getElementById('fileName').innerHTML=res>
</cffunction>
</cfclient>
<div id=&quot;fileName&quot;/>
<img style = &quot;width:200; height:100;&quot; id = &quot;myimg&quot;></img>
<button onclick=&quot;invokeCFClientFunction('getPicCam',null)&quot;>getPicture - JPEG</button>

異なるソースからの画像の取得

<cfclientsettings enableDeviceAPI=true>

<cfclient>

<!--- デバイスのカメラからの写真の取得 --->
<cffunction access=&quot;public&quot; name=&quot;getPiccam&quot; returntype=&quot;void&quot; >
<cfset opt = cfclient.camera.getOptions()>
<cfset res = cfclient.camera.getPicture(opt,true)>
<cfset res='data:image/jpeg;base64,'+res>
<cfset document.getElementById('myimg').src=#res#>
</cffunction>

<!--- アルバムからの写真の取得 --->
<cffunction access=&quot;public&quot; name=&quot;getPicalbm&quot; returntype=&quot;void&quot; >
<cfset opt = cfclient.camera.getOptions()>
<cfset res = cfclient.camera.getPictureFromAlbum(opt,true)>
<cfset res='data:image/jpeg;base64,'+res>
<cfset document.getElementById('myimg').src=#res#>
</cffunction>

<!--- フォトライブラリからの写真の取得 --->
<cffunction access=&quot;public&quot; name=&quot;getPiclib&quot; returntype=&quot;void&quot; >
<cfset opt = cfclient.camera.getOptions()>
<cfset res = cfclient.camera.getPictureFromPhotoLibrary(opt,true)>
<cfset res='data:image/jpeg;base64,'+res>
<cfset document.getElementById('myimg').src=#res#>
</cffunction>

</cfclient>
<img style = &quot;width:300; height:300;&quot; id = &quot;myimg&quot;></img>
<button onclick=&quot;invokeCFClientFunction('getPiccam',null)&quot;>Camera</button><br>
<button onclick=&quot;invokeCFClientFunction('getPicalbm',null)&quot;>album</button><br>
<button onclick=&quot;invokeCFClientFunction('getPiclib',null)&quot;>Library</button><br>

カメラオプションの設定

<cfclientsettings enableDeviceAPI=true>

<cfclient>
 
<cffunction access=&quot;public&quot; name=&quot;setopt&quot; returntype=&quot;void&quot; >
<cfset options = {&quot;quality&quot; = &quot;40&quot;, &quot;encodingType&quot; = &quot;png&quot;}>
<cfset cfclient.camera.setOptions(options)>
</cffunction>
 
<button onclick=&quot;invokeCFClientFunction('setopt',null)&quot;>Set Camera Options</button>

</cfclient>

カメラオプションの取得

<cfclientsettings enableDeviceAPI=true>

<cfclient>
 
<cffunction access=&quot;public&quot; name=&quot;getopt&quot; returntype=&quot;void&quot; >
<cfset res = cfclient.camera.getOptions()>
</cffunction>
 
<button onclick=&quot;invokeCFClientFunction('getopt',null)&quot;>Get Camera Options</button>

</cfclient>

画像の格納

<cfclientsettings enableDeviceAPI=true>

<cfclient>
<cftry>
<cfset opt = cfclient.camera.getOptions()>
<cfset res = cfclient.camera.getPicture(opt,false)> 
<cfif !cfclient.file.directoryExists('mydir')> 
<cfset cfclient.file.createDirectory('mydir')>
</cfif>
<cfif opt.encodingtype eq &quot;jpeg&quot;>
<cfset cfclient.file.copy(res,'mydir/img.jpg')>
<cfif cfclient.file.exists('mydir/img.jpg')>
<cfset filObj=cfclient.file.get('mydir/img.jpg')>
<cfset document.getElementById('result').innerHTML=#filObj.fullPath#>
</cfif>
<cfelse>
<cfset cfclient.file.copy(res,'mydir/img1.png')>
<cfif cfclient.file.exists('mydir/img1.png')>
<cfset filObj=cfclient.file.get('mydir/img1.png')>
<cfset document.getElementById('result').innerHTML=#filObj.fullPath#>
</cfif>
</cfif>
<cfcatch type=&quot;any&quot;>
<cfset alert(cfcatch.message)>
</cfcatch>
</cftry>

</cfclient>
<div id=&quot;result&quot;/>

クリーンアップ

<cfclientsettings enableDeviceAPI=true>

<cfclient>
 
<cffunction access=&quot;public&quot; name=&quot;cleanup&quot; returntype=&quot;void&quot; >
<cftry>
<cfset cfclient.camera.cleanup()>

<cfcatch type=&quot;any&quot;>
<cfset alert(cfcatch.message)>
</cfcatch>
</cftry>
</cffunction>
 
<button onclick=&quot;invokeCFClientFunction('cleanup',null)&quot;>Cleanup now</button><br><br>
 
</cfclient>

ヘルプをすばやく簡単に入手

新規ユーザーの場合