基本上我正在做一個最佳圖像大小的屏幕截圖應用程序。從XCode中的Objective C中使用libjpeg
我想要捕獲我的Mac屏幕並將其保存爲JPEG文件。 我想用Cocoa API捕捉屏幕[CGWindowListCreateImage]並使用libjpeg9將文件保存爲JPEG格式。我使用XCode中的「Add Files to Project_Name」選項包含靜態庫[libjpeg.a]和頭文件[jpeglib.h,jconfig.h,jerror.h和jmorecfg.h]。
我用來保存爲JPEG文件的代碼是
int write_jpeg_file(char *filename)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
/* this is a pointer to one row of image data */
JSAMPROW row_pointer[1];
FILE *outfile = fopen(filename, "wb");
if (!outfile)
{
printf("Error opening output jpeg file %s\n!", filename);
return -1;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
/* Setting the parameters of the output file here */
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = bytes_per_pixel;
cinfo.in_color_space = color_space;
/* default compression parameters, we shouldn't be worried about these */
jpeg_set_defaults(&cinfo);
/* Now do the compression .. */
jpeg_start_compress(&cinfo, TRUE);
/* like reading a file, this time write one row at a time */
while(cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
/* similar to read file, clean up after we're done compressing */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose(outfile);
/* success code is 1! */
return 1;
}
當我生成和運行項目我收到以下錯誤
解析問題
預計}
在線
typedef enum {FALSE = 0,TRUE = 1} boolean;
在jmorecfg.h這是libjpeg頭文件。我不知道爲什麼我得到這個錯誤。請幫幫我。
我知道有一種方法可以使用Cocoa API來保存文件,而不是使用libjpeg。 我第一次嘗試使用Cocoa的XCode。我能夠獲得JPEG文件。文件大小相對較大(〜200KB)。當我使用libjpeg在C中創建圖像時,文件大小爲〜100KB。
的代碼我使用:
CGFloat imageCompression = 0.6;
- (NSImage *)captureImageForRect:(NSRect)rect {
CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:screenShot];
NSLog(@"Bits per pixel : %ld", [imageRep bitsPerPixel]);
NSImage *result = [[NSImage alloc] init];
[result addRepresentation:imageRep];
screenShot=nil;
imageRep=nil;
return result;
}
-(NSData *)jpegReresentationOfImage:(NSImage *) image {
NSBitmapImageRep* myBitmapImageRep=nil;
NSSize imageSize = [image size];
[image lockFocus];
NSRect imageRect = NSMakeRect(0, 0, imageSize.width, imageSize.height);
myBitmapImageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
[image unlockFocus];
// set up the options for creating a JPEG
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:imageCompression], NSImageCompressionFactor,
[NSNumber numberWithBool:NO], NSImageProgressive,
nil];
NSData* imageData = [myBitmapImageRep representationUsingType:NSJPEGFileType properties:options];
myBitmapImageRep=nil;
return (imageData);
}
-(void)captureAndSave {
NSString *fileName = @"/Volumes/Official/images/output/Mac_Window_1.jpg";
NSImage *screenshot = [self captureImageForRect:[[NSScreen mainScreen] frame]];
NSData *jpgRep = [self jpegReresentationOfImage:screenshot];
[jpgRep writeToFile:[fileName stringByExpandingTildeInPath] atomically:NO];
[NSApp terminate:self];
}
總之,如何在XCode中使用libjpeg? –