图像到文件的转换 [英] Image to file conversion

查看:107
本文介绍了图像到文件的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在编程方面很新鲜,希望我的问题能引起您的注意.
我正在研究将图像转换为字节的程序[]
我想要字节码信息,以便我可以将其用于进一步处理
所以我将字节转换为字符串并将其写入.txt文件

Hi, I''m a total freshy in programming, hope my question make sence.
I''m working on a program which converts image to byte[]
I want the byte code information so that i can use it for further processing
so i convert the byte to string and wrote it into a .txt file

但是我得到了一些类似的东西,而不是预期的二进制数:

But instead of expected binary numbers, i get these kinda stuff:

㼿①㼍㽰㼥㼢⌿㼈Вୂ⠿♲㼈㼸ሴ㽦䕖㼿ᄿ㼿㼿甞Ԛ䨿㽂㼿㽗㽳㴿፮倿᰿㼐搿㼿〿ℿ㼈晠᥌焈䁮戌㼿ਬ
└㑿ቮ〿䐿␿簿ᄤ㽁⥾㼍㼐ࠨᠢⴿ愄㼸ᡒ㼢䀿͖㼹㼹㽢䐿㼿㼿㼿ᠿ㼿㽳㼿㼿㼿㼿挗㼎ဓ࠿㼜ℜ㼀᝸㼃ᠿ㼿㼿п㼱
簿䀋ᴿ䀿ᐿ堂ᅈ㼿㼿ਿ㼖㽳儿∎愔⌿䀃࠹㽡㽝吿ȕ␈䌄砸㼿⌿㼃ᨃ
ℍ ⸸兆┿〿㽄稿㼳㽸Ȕ㤼ㄍ㔿䈿欿㼿㼝ᨁ㼣㼿㼋䜿㼿ဿ硁ᱡ㼂㼿桩㼿㽀㼕嘴㼿ᅥ㽤㼿ᡄݐ婤㼎㽹̡㤿㼃
䙄㼸戋怿焋㴿᠇ᠿ䁂㼰㼿䈓尨ℊ⌿㼄ᴿ㽃᜿扛㼿᰿㼼㽓䈖㼿㼏̿ဿ฿㽁㽟㼿đ㥨愍眿㼿ㄿ⠿㼿㽳㽶㽱Ġ刿䄈
ဿ㼿㼽是∊瀿Kᔿ恁၏㼿㼭㼐㼙㼀愋ℼ瀃ါ်㼿ٽ㽀З怿㼎夐⌿㼿傧㜿砿ሿ㽂㽻㼿ܿ㼿㼅൘㼁ᤸ᥅㴿㼓隐藏㼿

㼿①㼍㽰㼥㼢⌿㼈Вୂ⠿♲㼈㼸ሴ㽦䕖㼿ᄿ㼿㼿甞Ԛ䨿㽂㼿㽗㽳㴿፮倿᰿㼐搿㼿〿ℿ㼈晠᥌焈䁮戌㼿ਬ
└㑿ቮ〿䐿␿簿ᄤ㽁⥾㼍㼐ࠨᠢⴿ愄㼸ᡒ㼢䀿͖㼹㼹㽢䐿㼿㼿㼿ᠿ㼿㽳㼿㼿㼿㼿挗㼎ဓ࠿㼜ℜ㼀᝸㼃ᠿ㼿㼿п㼱
㼿ᄿ㼿ͪ渿㼔兘䀋␿瀿฿㼁⁣硁ࠞ㽀㼅簿䀋ᴿ䀿ᐿ堂ᅈ㼿㼿ਿ㼖㽳儿∎愔⌿䀃࠹㽡㽝吿ȕ␈䌄砸㼿⌿㼃ᨃ
ℍ⸸兆┿〿㽄稿㼳㽸Ȕ㤼ㄍ㔿䈿欿㼿㼝ᨁ㼣㼿㼋䜿㼿ဿ硁ᱡ㼂㼿樁㼿㽀㼕嘴㼿ᅥ㽤㼿ᡄݐ婤㼎㽹̡㤿㼃
䙄㼸戔怿焋㴿᠇ᠿ䁂㼰㼿䈓尨ℊ⌿㼄ᴿ㽃᜿摃㼿᰿㼼㽓䈖㼿㼏̿ဿ฿㽁㽟㼿đ㥨愍眿㼿ㄿ⠿㼿㽳㽶㽱Ġ刿䄈
ဿ㼿㼽昰∊瀿Kᔿ恁၏㼿㼭㼐㼙㼀愋ℼ瀃ၚ㼿ٽ㽀З怿㼎夐⌿㼿儐㜿砿ሿ㽂㽻㼿ܿ㼿㼅൘㼁ᤸ᥅㴿㼓匿㼿


下面是我的C#代码:


Below is my C# code:

//此函数将图像转换为字节数组,然后存储到称为haha的byte []中.
byte [] haha​​ = imageToByteArray(pictureBox1.Image);

//这部分操作将字节数组转换为称为hihi的字符串.
System.Text.ASCIIEncoding enc =新的System.Text.ASCIIEncoding();
hihi = enc.GetString(haha);

//这部分操作将hihi写入.txt文件
string codetxtfile ="C:/Users/Trainee/Desktop/Test.txt";
System.IO.StreamWriter objWriter;
objWriter =新的System.IO.StreamWriter(codetxtfile);
objWriter.Write(hihi);
objWriter.Close();

//this function convert image to byte array then stored to byte[] called haha
byte[] haha = imageToByteArray(pictureBox1.Image);

//this part of the operation convert byte array to string called hihi
string hihi;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
hihi = enc.GetString(haha);

//this part of the operation write hihi into .txt file
string codetxtfile = "C:/Users/Trainee/Desktop/Test.txt";
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(codetxtfile);
objWriter.Write(hihi);
objWriter.Close();

MessageBox.Show(完成!");

MessageBox.Show("Done!");

如何将图像转换为字节并以.txt文件中的二进制代码显示?请

How do i convert the image to byte and show it in binary code in .txt file? Please

问题更新:


我正在尝试将8位深度灰度图像(256种黑白到黑白)上的像素颜色数据转换为数字,我希望这些数字是根据像素正确排列,例如将图像中的像素行转换为数字行.


I''m trying to convert the pixel color data on a 8 bit depth grayscale image(256 types of black to white) to numbers, i want these numbers to be in proper arrangement according to the pixel, as in lines of pixels in the image will be converted to line of numbers.

例如.

0 =黑色,255 =白色,1-254 =黑色和白色之间的中间灰色

0= black, 255= white , 1-254= intermediate grey between black and white

所以,用于灰度图像

我希望生成的代码像这样:

i want the generated code to be like:

0811FF00 ......(以十六进制表示),其中08 = 8th grey,11 = 17th grey,FF = white和00 = black

0811FF00...... (in hexadecimal), where 08= 8th grey, 11= 17th grey, FF=white, and 00= black

00001000000101111111111100000000 .....(二进制代码),其中00001000 = 8th grey,00010111 = 17th grey,11111111 = white,00000000 = black

00001000000101111111111100000000..... (in binary code), where 00001000= 8th grey, 00010111=17th grey, 11111111= white, and 00000000= black

以前,我认为图像到字节的转换将给出二进制数的数组(每个数字代表一个像素深度),我想我是错的.

Previosly, i thought image to byte conversion will give arrays of binary numbers(each represent a pixel depth), i guess i was wrong.

有人可以指导我吗?

推荐答案

我正在开发一个程序,可将图像转换为字节[]
I''m working on a program which converts image to byte[]

 

如果您阅读我的图像处理文章,就会发现如何读取图像的字节.但是,我仍然坚持认为,如果您是编程的新手,这是一个疯狂的任务.您为此得到报酬,是上学还是为自己设定的任务不符合自己的利益?

If you read my image processing articles, you''ll find how to read the bytes of an image.  However, I still maintain that if you''re totally new to programming, this is an insane task.  Are you being paid for this, is it for school, or are you setting yourself a task that is unreasonable for your own interest?

此外,您意识到,如果您编辑了原始问题,它将跳到队列的顶部?这是巩固IMO讨论的更好的方法.

In addition, you realise if you edited your original question, it would jump to the top of the queue ? That''s a much better way to consolidate a discussion IMO.


有关此背景的更多信息,请查看
For further background on this, check out the previous question. Notice that this person seems to be working with grayscale, which can be represented by a byte (values 0-255)

You might want to tell us more about what you are trying to accomplish. Are you trying to convert the pixel color data to numbers, then convert those numbers to human readable text? Or, are you trying to convert the pixel color data to numbers, then store those numbers to a file as byte data (which would not be human readable, but which could be loaded into a program later on)? Seems like you may be confusing the two.


我不知道C#,但也许您可以翻译我多年以前使用的VB5旧代码.我认为它将完成您要尝试执行的操作.运作方式如下:

I don''t know C#, but maybe you can translate this old VB5 code I did many years ago. I think it will do what you''re trying to do. Here''s the way it works:

  • 打开图片文件以进行二进制输入
  • 读取一个字符串(我选择一次读取16个字节,但是您可以根据需要进行设置.
  • convert字符串中的每个字节以其十六进制表示形式并将其添加到输出字符串中.
  • 循环遍历输入字符串,直到所有字节都已转换
  • 将输出字符串写入文件
  • 循环从输入文件中读取下一个字节字符串.
  • open the picture file for binary input
  • read a string of bytes (I chose to read in 16 at a time, but you can make it anything you want.
  • convert each byte in the string to its hex representation and add it to an output string.
  • loop through the input string until all bytes have been converted
  • write the output string to a file
  • loop to read the next string of bytes from the input file.

以下是VB代码:

Here''s the VB code:

将txtIn视作字符串,将txtOut视作字符串,将txtChr视作字符串
昏暗为整数

Dim txtIn As String, txtOut As String, txtChr As String
Dim n As Integer

为二进制文件#1打开"C:\ Picture1.tif"
打开"c:\ temp \ HEXOut.txt",将其输出为#2

Open "C:\Picture1.tif" For Binary As #1
Open "c:\temp\HEXOut.txt" For Output As #2

``循环直到没有更多内容可从输入文件中读取

''Loop until there is no more to read from the input file

不要LOF(1)时做

''从输入(图片)文件(#1)中读取16个字节
txtIn = Input(16,#1)

''read 16 bytes from the input (picture) file (#1)
txtIn = Input(16, #1)

''将txtOut初始化为空字符串

''initialize txtOut to an empty string

txtOut ="

txtOut = ""

"在转换每个字符(字节)时遍历txtIn

''Loop through txtIn while converting each character (byte)

对于n = 1到Len(txtIn)

For n = 1 To Len(txtIn)

"此行使用了VB5内置的三个功能:

''This line uses three functions built into VB5:

''1. Mid()返回字符串中的各个字符.在这种情况下,它将在txtIn字符串的第n个位置返回单个字符(1)

'' 1. Mid() returns individual characters in a string. In this case it returns a single character (1) at the nth position in the txtIn string

''2. Asc()返回单字符字符串的ASCII代码.

'' 2. Asc() returns the ASCII code for a single-character string.

''3. Hex()将十进制值转换为其十六进制表示形式,并将其作为字符串返回

'' 3. Hex() converts a decimal value to its hex representation and returns it as a character string

txtChr =十六进制(Asc(Mid(txtIn,n,1)))

txtChr = Hex(Asc(Mid(txtIn, n, 1)))

''我在十六进制数字<之前添加了一个前导零. 16(0-F).否则,它们将被表示为个位数并且难以阅读.

''I add a leading zero to hex numbers < 16 (0-F). Otherwise they would be represented as single digits and be harder to read.

如果Len(txtChr)< 2然后txtChr ="0"& txtChr

If Len(txtChr) < 2 Then txtChr = "0" & txtChr

''将十六进制字符添加到输出字符串中

''add the HEX characters to the output string

txtOut = txtOut& txtChr

txtOut = txtOut & txtChr

下一个n

"该行仅向该行添加回车符和换行符,以使文件更易于阅读.输出文件包含16个2个字符字节的行.

''This line just adds a carriage return and linefeed to the line to make the file easier for humans to read. The output file contains lines of 16 two-character bytes.

txtOut = txtOut& vbCrLf

txtOut = txtOut & vbCrLf

''将行写入输出文件

打印#2,txtOut

Print #2, txtOut

"循环直到读取完整个文件.

''Loop until the whole file has been read.

循环

关闭

我希望这会有所帮助.抱歉,我无法翻译.这些日子之一,我可能会尝试学习C#,但今天不是.

I hope this helps. Sorry I couldn''t translate it. One of these days I might try to learn C#, but not today.

-tom-


这篇关于图像到文件的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆