=================================
=================================
=================================
출처: http://stackoverflow.com/questions/2106195/loading-bmp-bitmap-and-tiff-file-in-flash-10-using-a-loader
I am developing a Image uploader using Flash 10. I am using FileReference
object to browse images and Loader to show the image:
Sample code:
var tempFileRef:FileReference = FileReference(ev.target); var oLoader:Loader = new Loader(); oLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad); oLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError); oLoader.loadBytes(tempFileRef.data);
It works fine with .jpg and .gif files but when I browse .bmp or .tiff file, I am getting the error:
Error #2124: Loaded file is an unknown type.
Is there a way to load and display browsed .bmp images from desktop?
Thanks
---------------------------------------------------------------------------------------------------------------------------
BMPDecoder class from Spark lib:http://www.libspark.org/svn/as3/BMPDecoder/src/com/voidelement/images/BMPDecoder.as
Example of the usage: http://hi.baidu.com/leyhui/blog/item/d12be8b116ec915308230298.html
Regarding TIFFDecoder, it is still work in progress, but very promising one:
http://code.google.com/p/windowsbitmapdencoder/
=================================
=================================
=================================
출처: http://blog.flashplatform.kr/201
오랫만에 블로그로 인사드립니다. (__) 최근에 BMP 파일을 Flex 에서 읽어다 쓸 일이 생겼습니다. Flex에서 이미지를 보여주는 컴포넌트인 Image 컴포넌트는 JPEG, PNG, GIF, SWF, SVG 같은 확장자만을 지원하고 BMP 은 지원하고 있지 않기 때문에 BMP을 바로 넣을 수는 없습니다. (웹에서 BMP 를 쓰는 무식한 짓을 하면 안되겠죠.. ㅜㅜ 크기도 큰데 말입니다.) JPG 파일이면 Image 컴포넌트에서 바로 읽어다 쓸 수 있을텐데 참 난감합니다. 그럼 방법이 없느냐? 아닙니다. 어떤 멋진 분께서 BMPDecoder 를 만들어놓으셨네요! :) 이미 FlexComponent 카페 에서도 공유되었던 내용 입니다.
http://ntt.cc/2008/10/01/using-bmpdecoder-class-to-load-an-external-bmp-file-rle-compression-support.html
위의 블로그에 들어가보시면 간단한 사용법에 대해서 나오고 BMPDecoder 라이브러리도 보입니다. 다만 BMPDecoder.as 파일이 다운로드가 안되네요. BMPDecoder.as 파일은 ' 삶의 향기 '라는 블로그에 간단한 예제와 함께 업로드 되어 있으니 다운받아 사용하시면 되겠습니다. 간단하게 FP10 에서 추가된 FileReference의 load() 메소드를 이용해, BMP파일을 불러다 Image 컴포넌트에 넣는 예제를 만들어봤습니다. 예제 구성은 위에서 소개해드린 '삶의 향기' 블로그에 소개된 코드를 이용했습니다.
<?xml version= "1.0" encoding= "utf-8" ?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout= "absolute" creationComplete= "{init()}"
backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors= "[#FFFFFF, #FFFFFF]" >
<mx:Script>
<![CDATA[
import mcmall.core.codec.BMPDecoder;
private var fileRef:FileReference;
private function init(): void
{
fileRef = new FileReference();
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(Event.SELECT, selectHandler);
}
private function bmpLoad(): void
{
fileRef.browse([new FileFilter( "BMP" , "*.bmp" )]);
}
private function completeHandler(event:Event): void {
var byteArray:ByteArray = new ByteArray();
fileRef.data.readBytes(byteArray);
var bmpDecoder:BMPDecoder = new BMPDecoder();
var tempBitmap:Bitmap = new Bitmap();
tempBitmap.bitmapData = bmpDecoder.decode( byteArray );
imgBmp.load(tempBitmap);
imgBmp.width = imgBmp.contentWidth;
imgBmp.height = imgBmp.contentHeight;
}
private function selectHandler(event:Event): void {
var file:FileReference = FileReference(event.target);
file.load();
}
]]>
</mx:Script>
<mx:VBox height="100%" width= "100%"
horizontalCenter="0" verticalCenter= "0"
verticalAlign="middle" horizontalAlign= "center" >
<mx:Image width="101" height= "145" id= "imgBmp" />
<mx:Button label = "BMP Load" click= "{bmpLoad()}" />
</mx:VBox>
</mx:Application>
BMPDecoder.as 보기
BMP 파일을 선택하시면 Image 컴포넌트에 깔끔하게 출력되는 것을 보실 수 있습니다. 하지만 다음과 같은 버그가 존재합니다.
170픽셀 16bit BMP
171픽셀 16bit BMP 파일
위의 이미지에서 볼수 있는 것 처럼 BMP을 로드했을 때
이미지가 뒤틀려버리는 버그 가 존재합니다.
왜 그럴까 한참을 고민했는데, 저희 팀장님이 힌트를 주시더군요. '
윈도우어플에서도 비트맵 크기가 홀수일 경우 이미지가 저렇게 깨진다 '고 말이죠. 실제로 테스트해보니 위와 같이
16bit 이고 이미지의
너비가 홀수 일 경우 위 처럼 이미지가 뒤틀려버리는 버그가 있습니다.
(24bit, 32bit에서는 괜찮더군요) 이 버그에 해결책으로 저는 아래와 같이
decode() 메소드 에서 이미지 너비를 결정해주는 로직 밑에
너비가 홀수일경우 강제로 1픽셀 늘리는 방법 을 사용했습니다. 더 좋은 방법이 있을 것 같은데, 아시는 분은 알려주세요 :)
public function decode(data:ByteArray):BitmapData
{
bytes = data;
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.position = 0;
readFileHeader();
nInfoSize = bytes.readUnsignedInt();
switch (nInfoSize)
{
case BITMAP_CORE_HEADER_SIZE:
readCoreHeader();
break ;
case BITMAP_INFO_HEADER_SIZE:
readInfoHeader();
break ;
default :
readExtendedInfoHeader();
break ;
}
if ( (nWidth % 2) == 1 ) nWidth += 1;
bd = new BitmapData(nWidth, nHeight);
...
그럼 좋은 하루되세요!
예제파일 다운로드 :
=================================
=================================
=================================
출처: http://ntt.cc/2008/10/01/using-bmpdecoder-class-to-load-an-external-bmp-file-rle-compression-support.html
1
function
loadBMPFile( url:
String
):
void
{
2
var
loader:URLLoader =
new
URLLoader();
3
loader.dataFormat = URLLoaderDataFormat.BINARY;
4
loader.addEventListener( Event.COMPLETE, onCompleteLoad );
5
loader.load(
new
URLRequest( url ) );
7
function
onCompleteLoad( e:Event ):
void
{
8
var
loader:URLLoader = e.target
as
URLLoader;
9
var
decoder:BMPDecoder =
new
BMPDecoder();
10
var
bd:BitmapData = decoder.decode( loader.data );
The following is source code of BMPDecoder class :
1
function
loadBMPFile( url:
String
):
void
{
2
var
loader:URLLoader =
new
URLLoader();
3
loader.dataFormat = URLLoaderDataFormat.BINARY;
4
loader.addEventListener( Event.COMPLETE, onCompleteLoad );
5
loader.load(
new
URLRequest( url ) );
7
function
onCompleteLoad( e:Event ):
void
{
8
var
loader:URLLoader = e.target
as
URLLoader;
9
var
decoder:BMPDecoder =
new
BMPDecoder();
10
var
bd:BitmapData = decoder.decode( loader.data );
13
The following
is
source code of BMPDecoder
class
:
15
package
com.voidelement.images {
16
import
flash.display.BitmapData;
17
import
flash.errors.IOError;
18
import
flash.utils.ByteArray;
19
import
flash.utils.Endian;
20
public
class
BMPDecoder {
22
private
const
BITMAP_HEADER_TYPE:
String
=
"BM"
;
23
private
const
BITMAP_FILE_HEADER_SIZE:
int
=
14
;
24
private
const
BITMAP_CORE_HEADER_SIZE:
int
=
12
;
25
private
const
BITMAP_INFO_HEADER_SIZE:
int
=
40
;
26
private
const
COMP_RGB :
int
=
0
;
27
private
const
COMP_RLE8 :
int
=
1
;
28
private
const
COMP_RLE4 :
int
=
2
;
29
private
const
COMP_BITFIELDS:
int
=
3
;
30
private
const
BIT1 :
int
=
1
;
31
private
const
BIT4 :
int
=
4
;
32
private
const
BIT8 :
int
=
8
;
33
private
const
BIT16:
int
=
16
;
34
private
const
BIT24:
int
=
24
;
35
private
const
BIT32:
int
=
32
;
37
private
var
bytes:ByteArray;
38
private
var
palette:
Array
;
39
private
var
bd:BitmapData;
40
private
var
nFileSize:
uint
;
41
private
var
nReserved1:
uint
;
42
private
var
nReserved2:
uint
;
43
private
var
nOffbits:
uint
;
44
private
var
nInfoSize:
uint
;
45
private
var
nWidth:
int
;
46
private
var
nHeight:
int
;
47
private
var
nPlains:
uint
;
48
private
var
nBitsPerPixel:
uint
;
49
private
var
nCompression:
uint
;
50
private
var
nSizeImage:
uint
;
51
private
var
nXPixPerMeter:
int
;
52
private
var
nYPixPerMeter:
int
;
53
private
var
nColorUsed:
uint
;
54
private
var
nColorImportant:
uint
;
55
private
var
nRMask:
uint
;
56
private
var
nGMask:
uint
;
57
private
var
nBMask:
uint
;
58
private
var
nRPos:
uint
;
59
private
var
nGPos:
uint
;
60
private
var
nBPos:
uint
;
61
private
var
nRMax:
uint
;
62
private
var
nGMax:
uint
;
63
private
var
nBMax:
uint
;
67
public
function
BMPDecoder() {
77
public
function
decode( data:ByteArray ):BitmapData {
79
bytes.endian = Endian.LITTLE_ENDIAN;
82
nInfoSize = bytes.readUnsignedInt();
83
switch
( nInfoSize ) {
84
case
BITMAP_CORE_HEADER_SIZE:
87
case
BITMAP_INFO_HEADER_SIZE:
91
readExtendedInfoHeader();
94
bd =
new
BitmapData( nWidth, nHeight );
95
switch
( nBitsPerPixel ){
102
if
( nCompression == COMP_RLE4 ){
110
if
( nCompression == COMP_RLE8 ){
130
throw
new
VerifyError(
"invalid bits per pixel : "
+ nBitsPerPixel );
137
private
function
readFileHeader():
void
{
138
var
fileHeader:ByteArray =
new
ByteArray();
139
fileHeader.endian = Endian.LITTLE_ENDIAN;
141
bytes.readBytes( fileHeader,
0
, BITMAP_FILE_HEADER_SIZE );
142
if
( fileHeader.readUTFBytes(
2
) != BITMAP_HEADER_TYPE ){
143
throw
new
VerifyError(
"invalid bitmap header type"
);
145
nFileSize = fileHeader.readUnsignedInt();
146
nReserved1 = fileHeader.readUnsignedShort();
147
nReserved2 = fileHeader.readUnsignedShort();
148
nOffbits = fileHeader.readUnsignedInt();
149
}
catch
( e:IOError ) {
150
throw
new
VerifyError(
"invalid file header"
);
156
private
function
readCoreHeader():
void
{
157
var
coreHeader:ByteArray =
new
ByteArray();
158
coreHeader.endian = Endian.LITTLE_ENDIAN;
160
bytes.readBytes( coreHeader,
0
, BITMAP_CORE_HEADER_SIZE -
4
);
161
nWidth = coreHeader.readShort();
162
nHeight = coreHeader.readShort();
163
nPlains = coreHeader.readUnsignedShort();
164
nBitsPerPixel = coreHeader.readUnsignedShort();
165
}
catch
( e:IOError ) {
166
throw
new
VerifyError(
"invalid core header"
);
172
private
function
readInfoHeader():
void
{
173
var
infoHeader:ByteArray =
new
ByteArray();
174
infoHeader.endian = Endian.LITTLE_ENDIAN;
176
bytes.readBytes( infoHeader,
0
, BITMAP_INFO_HEADER_SIZE -
4
);
177
nWidth = infoHeader.readInt();
178
nHeight = infoHeader.readInt();
179
nPlains = infoHeader.readUnsignedShort();
180
nBitsPerPixel = infoHeader.readUnsignedShort();
181
nCompression = infoHeader.readUnsignedInt();
182
nSizeImage = infoHeader.readUnsignedInt();
183
nXPixPerMeter = infoHeader.readInt();
184
nYPixPerMeter = infoHeader.readInt();
185
nColorUsed = infoHeader.readUnsignedInt();
186
nColorImportant = infoHeader.readUnsignedInt();
187
}
catch
( e:IOError ) {
188
throw
new
VerifyError(
"invalid info header"
);
194
private
function
readExtendedInfoHeader():
void
{
195
var
infoHeader:ByteArray =
new
ByteArray();
196
infoHeader.endian = Endian.LITTLE_ENDIAN;
198
bytes.readBytes( infoHeader,
0
, nInfoSize -
4
);
199
nWidth = infoHeader.readInt();
200
nHeight = infoHeader.readInt();
201
nPlains = infoHeader.readUnsignedShort();
202
nBitsPerPixel = infoHeader.readUnsignedShort();
203
nCompression = infoHeader.readUnsignedInt();
204
nSizeImage = infoHeader.readUnsignedInt();
205
nXPixPerMeter = infoHeader.readInt();
206
nYPixPerMeter = infoHeader.readInt();
207
nColorUsed = infoHeader.readUnsignedInt();
208
nColorImportant = infoHeader.readUnsignedInt();
209
if
( infoHeader.bytesAvailable >=
4
) nRMask = infoHeader.readUnsignedInt();
210
if
( infoHeader.bytesAvailable >=
4
) nGMask = infoHeader.readUnsignedInt();
211
if
( infoHeader.bytesAvailable >=
4
) nBMask = infoHeader.readUnsignedInt();
212
}
catch
( e:IOError ) {
213
throw
new
VerifyError(
"invalid info header"
);
219
private
function
readBitFields():
void
{
220
if
( nCompression == COMP_RGB ){
221
if
( nBitsPerPixel == BIT16 ){
232
}
else
if
( ( nCompression == COMP_BITFIELDS ) && ( nInfoSize <
52
) ){
234
nRMask = bytes.readUnsignedInt();
235
nGMask = bytes.readUnsignedInt();
236
nBMask = bytes.readUnsignedInt();
237
}
catch
( e:IOError ) {
238
throw
new
VerifyError(
"invalid bit fields"
);
245
private
function
readColorPalette():
void
{
247
var
len:
int
= ( nColorUsed >
0
) ? nColorUsed : Math.pow(
2
, nBitsPerPixel );
248
palette =
new
Array
( len );
249
for
( i =
0
; i < len; ++i ){
250
palette[ i ] = bytes.readUnsignedInt();
256
private
function
decode1BitBMP():
void
{
261
var
buf:ByteArray =
new
ByteArray();
262
var
line:
int
= nWidth /
8
;
264
line = ( ( line /
4
|
0
) +
1
) *
4
;
267
for
( y = nHeight -
1
; y >=
0
; --y ){
269
bytes.readBytes( buf,
0
, line );
270
for
( x =
0
; x < nWidth; x +=
8
){
271
col = buf.readUnsignedByte();
272
for
( i =
0
; i <
8
; ++i ){
273
bd.setPixel( x + i, y, palette[ col >> (
7
- i ) &
0x01
] );
277
}
catch
( e:IOError ) {
278
throw
new
VerifyError(
"invalid image data"
);
284
private
function
decode4bitRLE():
void
{
291
var
buf:ByteArray =
new
ByteArray();
293
for
( y = nHeight -
1
; y >=
0
; --y ){
295
while
( bytes.bytesAvailable >
0
){
296
n = bytes.readUnsignedByte();
299
data = bytes.readUnsignedByte();
300
for
( i =
0
; i < n/
2
; ++i ){
301
buf.writeByte( data );
304
n = bytes.readUnsignedByte();
307
bytes.readBytes( buf, buf.length, n/
2
);
309
if
( n/
2
+
1
>>
1
<<
1
!= n/
2
){
310
bytes.readUnsignedByte();
319
for
( x =
0
; x < nWidth; x +=
2
){
320
col = buf.readUnsignedByte();
321
bd.setPixel( x, y, palette[ col >>
4
] );
322
bd.setPixel( x +
1
, y, palette[ col &
0x0f
] );
325
}
catch
( e:IOError ) {
326
throw
new
VerifyError(
"invalid image data"
);
332
private
function
decode4BitBMP():
void
{
337
var
buf:ByteArray =
new
ByteArray();
338
var
line:
int
= nWidth /
2
;
340
line = ( ( line /
4
|
0
) +
1
) *
4
;
343
for
( y = nHeight -
1
; y >=
0
; --y ){
345
bytes.readBytes( buf,
0
, line );
346
for
( x =
0
; x < nWidth; x +=
2
){
347
col = buf.readUnsignedByte();
348
bd.setPixel( x, y, palette[ col >>
4
] );
349
bd.setPixel( x +
1
, y, palette[ col &
0x0f
] );
352
}
catch
( e:IOError ) {
353
throw
new
VerifyError(
"invalid image data"
);
359
private
function
decode8BitRLE():
void
{
366
var
buf:ByteArray =
new
ByteArray();
368
for
( y = nHeight -
1
; y >=
0
; --y ){
370
while
( bytes.bytesAvailable >
0
){
371
n = bytes.readUnsignedByte();
374
data = bytes.readUnsignedByte();
375
for
( i =
0
; i < n; ++i ){
376
buf.writeByte( data );
379
n = bytes.readUnsignedByte();
382
bytes.readBytes( buf, buf.length, n );
384
if
( n +
1
>>
1
<<
1
!= n ){
385
bytes.readUnsignedByte();
394
for
( x =
0
; x < nWidth; ++x ){
395
bd.setPixel( x, y, palette[ buf.readUnsignedByte() ] );
398
}
catch
( e:IOError ) {
399
throw
new
VerifyError(
"invalid image data"
);
405
private
function
decode8BitBMP():
void
{
410
var
buf:ByteArray =
new
ByteArray();
411
var
line:
int
= nWidth;
413
line = ( ( line /
4
|
0
) +
1
) *
4
;
416
for
( y = nHeight -
1
; y >=
0
; --y ){
418
bytes.readBytes( buf,
0
, line );
419
for
( x =
0
; x < nWidth; ++x ){
420
bd.setPixel( x, y, palette[ buf.readUnsignedByte() ] );
423
}
catch
( e:IOError ) {
424
throw
new
VerifyError(
"invalid image data"
);
430
private
function
decode16BitBMP():
void
{
435
for
( y = nHeight -
1
; y >=
0
; --y ){
436
for
( x =
0
; x < nWidth; ++x ){
437
col = bytes.readUnsignedShort();
438
bd.setPixel( x, y, ( ( ( col & nRMask ) >> nRPos )*
0xff
/nRMax <<
16
) + ( ( ( col & nGMask ) >> nGPos )*
0xff
/nGMax <<
8
) + ( ( ( col & nBMask ) >> nBPos )*
0xff
/nBMax <<
0
) );
441
}
catch
( e:IOError ) {
442
throw
new
VerifyError(
"invalid image data"
);
448
private
function
decode24BitBMP():
void
{
452
var
buf:ByteArray =
new
ByteArray();
453
var
line:
int
= nWidth *
3
;
455
line = ( ( line /
4
|
0
) +
1
) *
4
;
458
for
( y = nHeight -
1
; y >=
0
; --y ){
460
bytes.readBytes( buf,
0
, line );
461
for
( x =
0
; x < nWidth; ++x ){
462
bd.setPixel( x, y, buf.readUnsignedByte() + ( buf.readUnsignedByte() <<
8
) + ( buf.readUnsignedByte() <<
16
) );
465
}
catch
( e:IOError ) {
466
throw
new
VerifyError(
"invalid image data"
);
472
private
function
decode32BitBMP():
void
{
477
for
( y = nHeight -
1
; y >=
0
; --y ){
478
for
( x =
0
; x < nWidth; ++x ){
479
col = bytes.readUnsignedInt();
480
bd.setPixel( x, y, ( ( ( col & nRMask ) >> nRPos )*
0xff
/nRMax <<
16
) + ( ( ( col & nGMask ) >> nGPos )*
0xff
/nGMax <<
8
) + ( ( ( col & nBMask ) >> nBPos )*
0xff
/nBMax <<
0
) );
483
}
catch
( e:IOError ) {
484
throw
new
VerifyError(
"invalid image data"
);
490
private
function
checkColorMask():
void
{
491
if
( ( nRMask & nGMask ) | ( nGMask & nBMask ) | ( nBMask & nRMask ) ){
492
throw
new
VerifyError(
"invalid bit fields"
);
494
while
( ( ( nRMask >> nRPos ) &
0x00000001
) ==
0
){
497
while
( ( ( nGMask >> nGPos ) &
0x00000001
) ==
0
){
500
while
( ( ( nBMask >> nBPos ) &
0x00000001
) ==
0
){
503
nRMax = nRMask >> nRPos;
504
nGMax = nGMask >> nGPos;
505
nBMax = nBMask >> nBPos;
510
public
function
trace
Info():
void
{
511
trace
(
"---- FILE HEADER ----"
);
512
trace
(
"nFileSize: "
+ nFileSize );
513
trace
(
"nReserved1: "
+ nReserved1 );
514
trace
(
"nReserved2: "
+ nReserved2 );
515
trace
(
"nOffbits: "
+ nOffbits );
516
trace
(
"---- INFO HEADER ----"
);
517
trace
(
"nWidth: "
+ nWidth );
518
trace
(
"nHeight: "
+ nHeight );
519
trace
(
"nPlains: "
+ nPlains );
520
trace
(
"nBitsPerPixel: "
+ nBitsPerPixel );
521
if
( nInfoSize >=
40
){
522
trace
(
"nCompression: "
+ nCompression );
523
trace
(
"nSizeImage: "
+ nSizeImage );
524
trace
(
"nXPixPerMeter: "
+ nXPixPerMeter );
525
trace
(
"nYPixPerMeter: "
+ nYPixPerMeter );
526
trace
(
"nColorUsed: "
+ nColorUsed );
527
trace
(
"nColorUsed: "
+ nColorImportant );
529
if
( nInfoSize >=
52
){
530
trace
(
"nRMask: "
+ nRMask.toString(
2
) );
531
trace
(
"nGMask: "
+ nGMask.toString(
2
) );
532
trace
(
"nBMask: "
+ nBMask.toString(
2
) );
Cheers!
=================================
=================================
=================================
출처: http://forums.mediabox.fr/topic/99416-error-2044-ioerrorevent/
Bonjour, Je refais monter ce sujet après moultes recherches. Je suis toujours bloqué malgré les indications postées. J'ai le même type d'erreur pour un chargement dynamique d'images (retournées par une servlet).
Action Script
var chargeur:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://localhost:8080/TO/download"); //retourne un jpeg
chargeur.dataFormat = URLLoaderDataFormat.BINARY;
request.data = "file="+(value as FileVO).path;
chargeur.addEventListener(Event.COMPLETE, finDuChargement);
chargeur.addEventListener(ProgressEvent.PROGRESS, avancement);
chargeur.load(request);
function finDuChargement ( event:Event ) {
//removeChild(progressBar);
//removeChild(pic);
getChildIndex(pic)
var contenu = event.target.data;
creerLoader (contenu)
}
function creerLoader ( datas ) {
var chargeur:Loader = new Loader();
chargeur.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, indiquerErreur);
chargeur.loadBytes(datas);
addChild(chargeur);
}
Voilà l'erreur que je récupère dans la console : [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2124: Le type du fichier chargé est inconnu."] Je ne sais pas trop comment faire. Avez vous une piste ? Merci par avance.
=================================
=================================
=================================