ZBLOG

读取图片提示错误panic: interface conversion: color.Color is color.YCbCr, not color.RGBA

抱歉,这是因为在使用image.Decode解码图片时,您传入的图片格式不是RGBA格式。目前代码只支持处理RGBA格式的图片。

如果您要处理其他格式的图片,可以使用适当的库进行转换。例如,如果您想处理YCbCr格式的图片,可以使用golang.org/x/image/colorutil包中的函数将其转换为RGBA格式。以下是修改后的代码:

package main

import (
	"fmt"
	_ "image/jpeg"
	"image/color"
	"image"
	"os"
	"syscall"
	"unsafe"

	colorutil "golang.org/x/image/colorutil"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("请提供要显示的图片路径作为参数")
		return
	}

	imagePath := os.Args[1]
	width := 1920
	height := 1080

	imgFile, err := os.Open(imagePath)
	if err != nil {
		fmt.Println("无法打开图片文件:", err)
		return
	}
	defer imgFile.Close()

	img, _, err := image.Decode(imgFile)
	if err != nil {
		fmt.Println("无法解码图片文件:", err)
		return
	}

	bounds := img.Bounds()
	newImg := image.NewRGBA(bounds)

	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			oldPixelColor := img.At(x, y)
			newPixelColor := color.RGBAModel.Convert(oldPixelColor).(color.RGBA)
			newImg.Set(x, y, newPixelColor)
	    }
    }

	fbDevice := "/dev/fb0"

	fbHandle, err := syscall.Open(fbDevice, syscall.O_RDWR|syscall.O_SYNC, 0666)
	if err != nil {
		fmt.Println("无法打开帧缓冲设备:", err)
		return
	}
	defer syscall.Close(fbHandle)

	frameSizeBytes := width * height * 4 // 假设每个像素点占用4字节(RGBA)

	frameBufferAddrPtr, err := syscall.Mmap(
			fbHandle,
			int64(0),
			frameSizeBytes,
			syscall.PROT_WRITE|syscall.PROT_READ,
			syscall.MAP_SHARED)
	if err != nil {
	 	fmt.Println("无法映射帧缓冲设备:", err)
	 	return
    }
    defer syscall.Munmap(frameBufferAddrPtr)

    frameBufferSlice := (*[1<<30]byte)(unsafe.Pointer(&frameBufferAddrPtr[0]))[:frameSizeBytes:frameSizeBytes]

	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			offset := (y*width + x) * 4 // 每个像素点占用4字节(RGBA)
			pixelColor := newImg.At(x, y).(color.RGBA)
			frameBufferSlice[offset] = pixelColor.R
			frameBufferSlice[offset+1] = pixelColor.G
			frameBufferSlice[offset+2] = pixelColor.B
			frameBufferSlice[offset+3] = pixelColor.A
		}
	}

	fmt.Println("图片已成功写入到/dev/fb0设备")
}

请确保导入了golang.org/x/image/colorutil包,并使用该代码处理YCbCr格式的图片。如果您仍然遇到问题,请提供更多信息,以便我能够帮助您解决。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=16499

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?