Ich möchte eine JPEG-Datei in die Bitmap mit Objective C konvertieren. Wir müssen diese in png konvertieren, aber es gibt keine bestimmte Methode in Ziel c, um jpeg in Bitmap zu konvertieren.Wie konvertiert man eine JPEG-Datei nach BITMAP mit dem Ziel c?
1
A
Antwort
1
Zuerst erstellen Sie erhalten BGR-Array von JPEG-Bild. create Bitmap image header (alle bmp-Images haben denselben Header). Header an Byte-Array anhängen und dann BGR-Array wie B-Byte, dann G-Byte und dann Byte-ähnlich anhängen. Dann konvertiere endlich das Byte-Array in NSDATA. Sie erhalten Daten für das BMP-Bild. Dieser Code funktioniert für Bild mit 512 X 512 Pixel und im BMP-Bild wird in 24-Bit-BMP-Bild umgewandelt. Im Code SIDE = 512. Imgpath wird wie "users/admin" sein und destimgpath wird wie "users/admin/bmpfolder" sein. Dateiname wird wie 1.jpg
-(void) convertJpegToBmp: (NSString *)imgDirPath :(NSString *)destImgPath :(NSString *)fileName
{
NSString *jpegimagepath = [imgDirPath stringByAppendingPathComponent:fileName];
NSString *fileNameWithoutExtension=[[fileName lastPathComponent] stringByDeletingPathExtension];
NSString *bmpFileName=[fileNameWithoutExtension stringByAppendingString:@".bmp"];
NSString *bmpfilepath=[destImgPath stringByAppendingPathComponent:bmpFileName];
int totalbytesinbitmap=(int) (SIDE * SIDE * 3)+54;
Byte bytes[totalbytesinbitmap];
[self writeBitmapHeader:bytes];
UIImage *sourceImage1=[UIImage imageWithContentsOfFile:jpegimagepath];
if(sourceImage1==nil)
return;
CGImageRef sourceImage = sourceImage1.CGImage;
CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));
UInt8 *pixelDataS = (UInt8 *) CFDataGetBytePtr(theData);
int dataLength = (int)CFDataGetLength(theData);
int k=54;
int row=(int) SIDE -1 ;
int col=0;
int totalbytesinrow=(int) SIDE * 3;
for(int i=0;i<dataLength;){
int red=pixelDataS[i++];
int green=pixelDataS[i++];
int blue=pixelDataS[i++];
i++;
if(col==totalbytesinrow){
col=0;
row--;
}
int location=(row * totalbytesinrow)+col+54;
bytes[location]=blue;
col++;
k++;
bytes[location+1]=green;
col++;
k++;
bytes[location+2]=red;
col++;
k++;
}
NSData *TxData = [NSData dataWithBytesNoCopy:bytes length:totalbytesinbitmap freeWhenDone:NO];
[TxData writeToFile:bmpfilepath atomically:YES];
}
-(void) writeBitmapHeader:(Byte *) pixelData{
int BITMAPHEADER_SIZE= 14;
int BITMAPINFOHEADER_SIZE=40;
Byte bfType[]={ (Byte)'B', (Byte)'M'};
int bfSize=0;
int bfReserved1=0;
int bfReserved2=0;
int bfOffBits=BITMAPHEADER_SIZE+BITMAPINFOHEADER_SIZE;
int biSize = BITMAPINFOHEADER_SIZE;
int biWidth = 0;
int biHeight = 0;
int biPlanes = 1;
int biBitCount = 24;
int biCompression = 0;
int biSizeImage = 0x030000;
int biXPelsPerMeter = 0x0;
int biYPelsPerMeter = 0x0;
int biClrUsed = 0;
int biClrImportant = 0;
int parWidth=(int)SIDE;
int parHeight =(int)SIDE;
int pad= (4 -((parWidth * 3) %4)) * parHeight;
if((4 -((parWidth * 3) %4))==4)
{
pad=0;
}
biSizeImage= ((parWidth * parHeight) *3)+pad;
bfSize= biSizeImage + BITMAPHEADER_SIZE+BITMAPINFOHEADER_SIZE;
biWidth=parWidth;
biHeight=parHeight;
int count=0;
pixelData[count++]=bfType[0];
pixelData[count++]=bfType[1];
count=[self intToDWord:bfSize :pixelData :count];
count=[self intToWord:bfReserved1 :pixelData :count];
count=[self intToWord:bfReserved2 :pixelData :count];
count=[self intToDWord:bfOffBits :pixelData :count];
count=[self intToDWord:biSize :pixelData :count];
count=[self intToDWord:biWidth :pixelData :count];
count=[self intToDWord:biHeight :pixelData :count];
count=[self intToWord:biPlanes :pixelData :count];
count=[self intToWord:biBitCount :pixelData :count];
count=[self intToDWord:biCompression :pixelData :count];
count=[self intToDWord:biSizeImage :pixelData :count];
count=[self intToDWord:biXPelsPerMeter :pixelData :count];
count=[self intToDWord:biYPelsPerMeter :pixelData :count];
count=[self intToDWord:biClrUsed :pixelData :count];
count=[self intToDWord:biClrImportant :pixelData :count];
}
-(int) intToWord :(int) parValue :(Byte *) pixelData :(int) count{
pixelData[count++]= (Byte) (parValue & 0x00ff);
pixelData[count++]= (Byte) ((parValue >> 8) & 0x00ff);
return count;
}
-(int) intToDWord :(int) parValue :(Byte *) pixelData :(int) count{
pixelData[count++]= (Byte) (parValue & 0x000000FF);
pixelData[count++]= (Byte) ((parValue >> 8) & 0x000000FF);
pixelData[count++]= (Byte) ((parValue >> 16) & 0x000000FF);
pixelData[count++]= (Byte) ((parValue >> 24) & 0x000000FF);
return count;
}
Verwandte Themen
- 1. C#: Wie konvertiert man BITMAP-Byte-Array in JPEG-Format?
- 2. Android: Wie konvertiert man ImageView in Bitmap?
- 3. Wie konvertiert man eine Bitmap in eine Datei Android?
- 4. Wie konvertiert man ein Bitmap-Bild in IntPtr in C#?
- 5. Grafik undefined Zeichnung zum Ziel-Bitmap C#
- 6. Wie konvertiert man diesen cURL-Befehl in das Ziel c
- 7. Wie konvertiert man eine Bitmap in Drawable in Android?
- 8. Wie konvertiert man ein Symbol in Bitmap in Android?
- 9. Wie konvertiert man eine .pptx nach .pdf mit Python
- 10. Wie verwendet man CNContactPickerViewController mit Ziel c?
- 11. Wie konvertiert man eine Textdatei nach Excel mit Python
- 12. So speichern Sie eine Bitmap nach dem Anwenden von Effekten
- 13. Wie übergibt man eine Bitmap von Java nach C++ mit JNI in Android Development?
- 14. Wie konvertiert man Bitmap in Bild <Bgr, Byte>
- 15. Wie konvertiert man eine gdi + Bitmap-ähnliche Struktur in eine HDC?
- 16. C# Wie man eine Datei nach dem Lesen streamwriter/reader
- 17. Wie konvertiert man android.media.Image in ein Bitmap-Objekt?
- 18. Wie konvertiert man ASCII-Daten nach EBCDIC in C#?
- 19. Wie konvertiert man DateTime von JSON nach C#?
- 20. Wie konvertiert man u_char * nach char [] in C
- 21. Wie konvertiert man diese Zeichenfolge in C# nach dezimal?
- 22. Wie konvertiert man PPT nach HTML in C#?
- 23. Wie liest man eine Bitmap in OCAML?
- 24. Wie ändert man den Wert einer Zelle nach dem Neuladen in Ziel c?
- 25. Wie konvertiert man eine ID2D1Bitmap in eine ImageSource
- 26. Wie konvertiert man eine Zeichenkette in eine Ganzzahl in C#
- 27. Android: Wie man eine Bitmap verschiebt?
- 28. Wie konvertiert man ein Byte-Array in eine Bitmap-Instanz (.NET)?
- 29. Bitmap-CURROPTS nach dem Laden und Speichern
- 30. Wie verwischt man eine Bitmap (Android)?
prüfen https://stackoverflow.com/questions/31242352/converting-jpeg-to-bitmap-in-ios-using-objective-c –