首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django,不序列化的坚持

Django,不序列化的坚持
EN

Stack Overflow用户
提问于 2020-02-05 15:56:24
回答 1查看 68关注 0票数 0

我正在做一个Django项目,它使用平板扫描仪。连接到扫描仪需要很长时间。所以我在寻找一种重复使用扫描仪实例的方法。

序列化似乎是解决这个问题的方法。不幸的是,我不能序列化或挑选扫描器实例。我经常遇到错误,告诉我序列化失败了。

是否有另一种方法来重复使用同一个扫描器实例进行多个扫描?后端魔术还是前端魔法?(注意,我对前端开发一无所知。)

我们可以作弊!

该项目将在本地计算机上脱机运行,根本没有internet或网络连接。这可能会提供其他不安全的选项。

我用来扫描的东西

  • 神智,用于访问扫描器的包
  • 丙酮-3-理智,一个用于理智的Python包装器
  • 图像扫描,扫描仪驱动程序
EN

回答 1

Stack Overflow用户

发布于 2020-02-11 11:51:17

环顾四周后,我发现了RPyC。本教程提供了一个工作示例,我让它在几分钟内运行。

服务启动并连接到扫描仪。多个客户端可以立即调用该服务并进行扫描。

代码语言:javascript
复制
import rpyc
import sane


class MyService(rpyc.Service):

    def __init__(self):
        self.scanner = Scanner()

    def on_connect(self, conn):
        pass

    def on_disconnect(self, conn):
        pass

    def exposed_scan_image(self):
        self.scanner.low_dpi_scan('scanner_service')


class Scanner(object):
    """
    The Scanner class is used to interact with flatbed scanners.
    """

    def __init__(self):
        sane.init()
        self.device_name = None
        self.error_message = None
        self.device = self.get_device()

    def get_device(self):
        """
        Return the first detected scanner and set the name.

        @return: sane.SaneDev
        """
        devices = sane.get_devices()
        print('Available devices:', devices)

        # Empty list means no scanner is connect or the connected scanner is
        # already being used
        if not devices:
            self.error_message = 'Scanner disconnect or already being used.'
            print(self.error_message)
            return None

        # open the first scanner you see
        try:
            device = sane.open(devices[0][0])
        except Exception as e:
            self.error_message = e
            print(e)
            return None

        brand = devices[0][1]
        model = devices[0][2]
        self.device_name = "{brand}_{model}".format(
            brand=brand,
            model=model
        )

        print("Connected to:", self.device_name)

        # set to color scanning mode, this is not always the default mode
        device.mode = 'color'

        return device

    def low_dpi_scan(self, file_name):
        """
        Scan at 300 dpi and store as jpeg.

        @param file_name: string
        """
        image = self.scan_image(300)
        image.save(file_name+'.jpeg')

    def scan_image(self, dpi):
        """
        Scan an image.

        @param dpi: integer
        @return: image file
        """
        self.device.resolution = dpi
        self.device.start()
        image = self.device.snap()

        return image


if __name__ == "__main__":
    from rpyc.utils.server import ThreadedServer
    t = ThreadedServer(MyService(), port=18861)
    t.start()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60079559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档