0

我想将不同的 PDF 页面转换为 png。之后,我遍历所有像素以搜索彩色像素。主要目标是获取 PDF 的彩色页面。对于大多数页面,它运行良好。但是在某些页面上,我用红色(字母左侧)和蓝色(字母右侧)着色了工件。
这是 PDF 的原件:( 来源:brebook.dePDF中的原件


这是 png 中转换后的字母:

PDF中的原件

我怎样才能防止这种丑陋的文物。我不能用这个彩色像素来使用我的整个想法。
这是将单个页面转换为 png 的代码:

//Path for saving colored Pages
NSURL *savePath = [self.homePath URLByAppendingPathComponent:@"brebook_temp/"];

//PDFDocument *thePDF = [[PDFDocument alloc] initWithURL:self.filename];
NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfURL:self.filename];

int coloredPages = 0;

for(int i = 0; i < self.getNumberOfPages; i++){
    @autoreleasepool {
        //set current page label to current page
        self.currentPage = i + 1;
        
        //set current page to i
        [img setCurrentPage:i];
        
        //create a new NSImage instance
        NSImage *singlePage = [NSImage new];
        
        //set actuall page
        [singlePage addRepresentation:img];
        
        //get "old" size
        NSSize oldSize = [singlePage size];
        
        //edit the size to 72dpi
        NSSize newSize;
        newSize.width = oldSize.width * 150/72;
        newSize.height = oldSize.height * 150/72;
        
        //make new image
        NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(newSize.width, newSize.height)];
        
        //write into new image
        [resizedImage lockFocus];
        [singlePage drawInRect: NSMakeRect(0, 0, newSize.width, newSize.height) fromRect: NSMakeRect(0, 0, oldSize.width, oldSize.height) operation: NSCompositeSourceOver fraction: 1.0];
        [resizedImage unlockFocus];
        
        //Set URL for single pages
        NSURL *pageFilename = [savePath URLByAppendingPathComponent: [NSString stringWithFormat:@"Seite_%d.png", i+1]];
        
        
        [[NSFileManager defaultManager] createFileAtPath: [pageFilename path]
                                                contents:[[NSBitmapImageRep imageRepWithData:[singlePage TIFFRepresentation]]
                                                          representationUsingType:NSPNGFileType properties:nil]
                                              attributes:nil];
        
        
        if([self getColoredPixelOfImageFromURL:pageFilename] > 0){
            coloredPages++;
            NSLog(@"SEITE %d -----------------------------------------------------------------------------------------------", i+1);
            _coloredPages = coloredPages;
            [self.coloredPagesList appendString:[NSString stringWithFormat:@"%d,",i+1]];
        }else{
            NSError *error = nil;
            [[NSFileManager defaultManager] removeItemAtURL:pageFilename error:&error];
            if(error){
                NSLog(@"%@", error);
            }
        }
        
        resizedImage = nil;
        singlePage = nil;
        
    }
}
[self.appTimer invalidate];

非常感谢您的帮助!!!

4

0 回答 0