本文介绍了“无法将预期类型与实际类型匹配"使用 Codec.BMP 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Haskell 的经验很少,我想写一个简单的光线追踪器来练习.因为我不想使用wxHaskell这样的GUI工具(我认为学习如何使用它们需要很多时间),所以我决定简单地将输出图像保存为BMP文件.但我在这里遇到了一个问题:

I have a very little experience in Haskell and I want to write a simple ray tracer for practice. Because I didn't want to use GUI tools like wxHaskell (I think it'll take a lot of time to learn how to use them), I decided to simply save the output image to BMP file. But I have a problem here:

module Main where

import Codec.BMP
import qualified Data.ByteString as BS

main = do
  Right bmp <- readBMP "grass.bmp"
  BS.putStrLn $ BS.take 4 $ unpackBMPToRGBA32 bmp

这里我只想获取图像的第一个像素并打印其 RGBA 值.但是我收到一个错误说

Here I just want to take first pixel of the image and print its RGBA values. But I get an error saying

Couldn't match expected type `BS.ByteString'
            with actual type `bytestring-0.9.2.1:Data.ByteString.Internal.ByteString'
In the return type of a call of `unpackBMPToRGBA32'
In the second argument of `($)', namely `unpackBMPToRGBA32 bmp'
In the second argument of `($)', namely
  `BS.take 4 $ unpackBMPToRGBA32 bmp'

我做错了什么?如何获取图像的像素并打印它们的值?

What am I doing wrong? How can I take the pixels of the image and print their values?

推荐答案

你安装了两个 bytestring 包,所以 unpackBMPToRGBA32 return ByteString frombytestring-0.9.2.1BS.putStrLn 需要来自其他版本的 ByteString.

You have two bytestring packages installed, so unpackBMPToRGBA32 return ByteString from bytestring-0.9.2.1, and BS.putStrLn expects ByteString from other version.

尝试 ghc-pkg list bytestring 列出所有安装的 bytestring 版本.

Try ghc-pkg list bytestring to list all bytestring versions installed.

解决方案可能是

  • 使用 ghc-pkg unregister bytestring-
  • 取消注册其中之一
  • 在构建时隐藏其中之一:ghc --make -hide-package bytestring-

这篇关于“无法将预期类型与实际类型匹配"使用 Codec.BMP 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:02