0

さまざまな PDF ページを png に変換したいと考えています。その後、すべてのピクセルを繰り返し処理して、色付きのピクセルを検索します。主な目標は、PDF のカラー ページを取得することです。ほとんどのページで、うまく機能します。しかし、一部のページでは、アーティファクトを赤 (文字の左側) と青 (文字の右側) に色付けしています。
これは PDF のオリジナルです: (ソース: brebook.de )原稿はPDFで


そして、これは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