main.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const parser = require('dicom-parser');
  2. const fsp = require('fs-promise');
  3. const iconv = require("iconv-lite")
  4. const filePath = '/Users/fuyu/Downloads/CgocGF1Cuu2AMYb5AIEtSKYTE8Q756.dcm';
  5. let test = async url => {
  6. const fileBuffer = await fsp.readFile(url);
  7. const dataSet = parser.parseDicom(fileBuffer);
  8. const charset = getCharSet(dataSet)
  9. const originalString = dataSet.string;
  10. dataSet.string = (tag, index) => {
  11. let value = originalString.call(dataSet, tag, index);
  12. if (value != null && value != 'undefined') {
  13. console.log('charset', charset)
  14. let _value = iconv.decode(value, 'UTF-8');
  15. // console.log()
  16. if (_value != null && _value != 'undefined') {
  17. return _value.replace(/['^']+/g, '')
  18. }
  19. return _value;
  20. } else {
  21. return value;
  22. }
  23. };
  24. let text = dataSet.string('x0008103e')
  25. console.log(text, iconv.decode(text, 'ISO_IR 100'))
  26. }
  27. const chatSetMap = {
  28. 'ISO_IR 6': 'UTF-8',
  29. 'ISO_IR 192': 'UTF-8',
  30. 'ISO_IR 100': 'ISO-8859-1',
  31. 'ISO_IR 101': 'ISO-8859-2',
  32. 'ISO_IR 109': 'ISO-8859-3',
  33. 'ISO_IR 110': 'ISO-8859-4',
  34. 'ISO_IR 144': 'ISO-8859-5',
  35. 'ISO_IR 127': 'ISO-8859-6',
  36. 'ISO_IR 126': 'ISO-8859-7',
  37. 'ISO_IR 138': 'ISO-8859-8',
  38. 'ISO_IR 148': 'ISO-8859-9',
  39. }
  40. const getCharSet = dataSet => {
  41. let specificCharacterSet = dataSet.string('x00080005');
  42. let charset = 'GB2312'
  43. console.log('specificCharacterSet', specificCharacterSet)
  44. if (specificCharacterSet) {
  45. charset = chatSetMap[specificCharacterSet] || charset
  46. }
  47. return charset
  48. }
  49. // GB2312
  50. // //xml文件编码方式,以支持中文显示。
  51. // if (!csetString.empty())
  52. // encString = "GB2312";
  53. test(filePath)