Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/ImageSplitter/CRTableViewController.m

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by zcode, Wed Feb 13 03:09:04 2013 UTC revision 2 by zcode, Fri Feb 15 07:01:49 2013 UTC
# Line 9  Line 9 
9  #import "CRTableViewController.h"  #import "CRTableViewController.h"
10  #import <Cocoa/Cocoa.h>  #import <Cocoa/Cocoa.h>
11  #import "CRDropView.h"  #import "CRDropView.h"
12    #import "NSImage+data.h"
13    #import "ZipArchive.h"
14    
15  @implementation CRTableViewController {  @implementation CRTableViewController {
16          NSMutableArray *_dataArray;          NSMutableArray *_dataArray;
17          IBOutlet CRDropView *mainView;          IBOutlet CRDropView *mainView;
18          IBOutlet NSTableView *_tableView;          IBOutlet NSTableView *_tableView;
         IBOutlet NSTextField *_textField;  
19          IBOutlet NSProgressIndicator *_indicator;          IBOutlet NSProgressIndicator *_indicator;
20  }  }
21  @synthesize _dirPath;  @synthesize _dirPath;
# Line 71  Line 72 
72          [_tableView reloadData];          [_tableView reloadData];
73  }  }
74    
75  NSImage* trimImageByRect(NSImage *image, NSRect trimRect) {  - (NSString*)imageTypeExp:(NSInteger)type {
76          NSBitmapImageRep* imageRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation] ];          NSString *exp = @"";
77          NSSize pointSize = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh);          switch (type) {
78                    case IMAGE_TYPE_JPEG:
79          [image setSize:pointSize];                          exp = @"jpg";
80                            break;
81          NSRect newRect = NSZeroRect;                  case IMAGE_TYPE_PNG:
82          newRect.size = trimRect.size;                          exp = @"png";
83          NSImage *newImage = [ [NSImage alloc] initWithSize:newRect.size];                          break;
84                    case IMAGE_TYPE_TIFF:
85          [newImage lockFocus];                          exp = @"tif";
86          [NSGraphicsContext saveGraphicsState];                          break;
87                    case IMAGE_TYPE_JPEG2000:
88          [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];                          exp = @"jpg";
89                            break;
90          [image drawAtPoint:NSZeroPoint fromRect:trimRect operation:NSCompositeSourceOver fraction:1.0];                  case IMAGE_TYPE_BMP:
91          [NSGraphicsContext restoreGraphicsState];                          exp = @"bmp";
92          [newImage unlockFocus];                          break;
93                    }
94          return newImage;          return exp;
95  }  }
96    
97  - (IBAction)buildButton:(id)sender {  - (IBAction)buildButton:(id)sender {
98          NSLog(@"pushed");          NSLog(@"pushed");
99          BOOL isDirectory;          BOOL isDirectory;
100            NSInteger imageType = [[NSUserDefaults standardUserDefaults] integerForKey:@"image type"];
101            NSInteger zipFlag = [[NSUserDefaults standardUserDefaults] integerForKey:@"zip flag"];
102            NSInteger sizeFlag = [[NSUserDefaults standardUserDefaults] integerForKey:@"fit size"];
103            int fitWidth = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"fit width"];
104            int fitHeight = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"fit height"];
105            double qualityJpeg2000 = [[NSUserDefaults standardUserDefaults] doubleForKey:@"jpeg2000 quality"];
106            double qualityJpeg = [[NSUserDefaults standardUserDefaults] doubleForKey:@"jpeg quality"];
107            
108            NSString *dir = NSTemporaryDirectory();
109            NSLog(@"dir %@", dir);
110            
111            if (0 == zipFlag) {
112                    NSInteger result;
113                    NSOpenPanel* openPanel;
114                    
115                    openPanel = [NSOpenPanel openPanel];
116                    [openPanel setCanChooseDirectories:YES];
117                    [openPanel setCanCreateDirectories:YES];
118                    result = [openPanel runModal];
119                    if(result == NSOKButton) {
120                            NSString*       dirPath = [[openPanel URL] path];
121                            BOOL isDirectory;
122                            if ([[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&isDirectory]) {
123                                    if (isDirectory) {
124                                            self._dirPath = dirPath;
125                                            dir = dirPath;
126                                    } else {
127                                            NSRunAlertPanel(@"info", @"set a folder",
128                                                                            nil, nil, nil);
129                                    }
130                            }
131                    }
132            } else {
133                    
134                    NSInteger result;
135                    NSSavePanel* savePanel;
136                    
137                    savePanel = [NSSavePanel savePanel];
138                    NSArray *allowedFileTypes = [NSArray arrayWithObjects:@"zip",@"'ZIP'",nil];
139                    [savePanel setAllowedFileTypes:allowedFileTypes];
140            
141                    [savePanel setCanCreateDirectories:YES];
142                    result = [savePanel runModal];
143                    if(result == NSOKButton) {
144                            NSString*       dirPath = [[savePanel URL] path];
145                            self._dirPath = dirPath;
146                    }
147            }
148    
149    
150            
151    
152    
153          [_indicator setDoubleValue:0];          [_indicator setDoubleValue:0];
154          if ([[NSFileManager defaultManager] fileExistsAtPath:self._dirPath isDirectory:&isDirectory]) {          if ([[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:&isDirectory]) {
155                  if (isDirectory) {                  if (isDirectory) {
156                          _indicator.minValue = 0;                          _indicator.minValue = 0;
157                          _indicator.maxValue = [mainView._files count] * 2;                          _indicator.maxValue = [mainView._files count] * 2;
# Line 117  NSImage* trimImageByRect(NSImage *image, Line 171  NSImage* trimImageByRect(NSImage *image,
171                                                                    
172                                  if (pointSize.width < pointSize.height) {                                  if (pointSize.width < pointSize.height) {
173                                          NSString *filename;                                          NSString *filename;
174                                          NSData *newData;                                          filename = [dir stringByAppendingFormat:@"/%06d.%@", count++, [self imageTypeExp:imageType]];
                                         NSDictionary *properties;  
                                         float quality;  
                                           
                                         filename = [self._dirPath stringByAppendingFormat:@"/%06d.jpg", count++];  
                                         quality = 0.86;  
175                                                                                    
176                                          properties = [NSDictionary                                          NSImage *newImage = image;
177                                                                    dictionaryWithObject:[NSNumber numberWithFloat:quality]                                          if (0 == sizeFlag) {
178                                                                    forKey:NSImageCompressionFactor];                                                  newImage = [image fitImageInSize:NSMakeSize(fitWidth, fitHeight)];
179                                          newData = [bitmapImageRep representationUsingType:NSJPEGFileType                                          }
180                                                                                                                     properties:properties];                                          switch (imageType) {
181                                          [newData writeToFile:filename atomically:YES];                                                  case IMAGE_TYPE_JPEG:
182                                                            [[newImage JPEGDataWithQuality:qualityJpeg] writeToFile:filename atomically:YES];
183                                                            break;
184                                                    case IMAGE_TYPE_PNG:
185                                                            [[newImage PNGDataWithInterlaced:YES] writeToFile:filename atomically:YES];
186                                                            break;
187                                                    case IMAGE_TYPE_TIFF:
188                                                            [[newImage TIFFRepresentation] writeToFile:filename atomically:YES];
189                                                            break;
190                                                    case IMAGE_TYPE_JPEG2000:
191                                                            [[newImage JPEG2000DataWithQuality:qualityJpeg2000] writeToFile:filename atomically:YES];
192                                                            break;
193                                                    case IMAGE_TYPE_BMP:
194                                                            [[newImage BMPData] writeToFile:filename atomically:YES];
195                                                            break;
196                                            }
197                                                                                    
198                                          progress += 2;                                          progress += 2;
199                                          [_indicator setDoubleValue:progress];                                          [_indicator setDoubleValue:progress];
200                                  } else {                                  } else {
201                                          for (int j = 0; j < 2; j++) {                                          for (int j = 0; j < 2; j++) {
202                                                  NSString *filename;                                                  NSString *filename;
203                                                  NSImage *newImage;                                                  filename = [dir stringByAppendingFormat:@"/%06d.%@", count++, [self imageTypeExp:imageType]];
                                                 NSData *newData;  
                                                 NSDictionary *properties;  
                                                 float quality;  
                                                   
204                                                  NSRect rc = NSMakeRect((pointSize.width / 2.0 * (1 - j)), 0, pointSize.width / 2.0, pointSize.height);                                                  NSRect rc = NSMakeRect((pointSize.width / 2.0 * (1 - j)), 0, pointSize.width / 2.0, pointSize.height);
205                                                  newImage = trimImageByRect(image, rc);                  
206                                                                                                    NSImage *newImage = [image trimImageByRect:rc];
207                                                  filename = [self._dirPath stringByAppendingFormat:@"/%06d.jpg", count++];                                                  if (0 == sizeFlag) {
208                                                  quality = 0.86;                                                          newImage = [newImage fitImageInSize:NSMakeSize(fitWidth, fitHeight)];
209                                                                                                    }
210                                                  newData = [newImage TIFFRepresentation];                                                  switch (imageType) {
211                                                  bitmapImageRep = [NSBitmapImageRep imageRepWithData:newData];                                                          case IMAGE_TYPE_JPEG:
212                                                  properties = [NSDictionary                                                                  [[newImage JPEGDataWithQuality:qualityJpeg] writeToFile:filename atomically:YES];
213                                                                            dictionaryWithObject:[NSNumber numberWithFloat:quality]                                                                  break;
214                                                                            forKey:NSImageCompressionFactor];                                                          case IMAGE_TYPE_PNG:
215                                                  newData = [bitmapImageRep representationUsingType:NSJPEGFileType                                                                  [[newImage PNGDataWithInterlaced:YES] writeToFile:filename atomically:YES];
216                                                                                                                             properties:properties];                                                                  break;
217                                                  [newData writeToFile:filename atomically:YES];                                                          case IMAGE_TYPE_TIFF:
218                                                                                                                    [[newImage TIFFRepresentation] writeToFile:filename atomically:YES];
219                                                  [newImage release];                                                                  break;
220                                                            case IMAGE_TYPE_JPEG2000:
221                                                                    [[newImage JPEG2000DataWithQuality:qualityJpeg2000] writeToFile:filename atomically:YES];
222                                                                    break;
223                                                            case IMAGE_TYPE_BMP:
224                                                                    [[newImage BMPData] writeToFile:filename atomically:YES];
225                                                                    break;
226                                                    }
227                                                                                                    
228                                                  progress++;                                                  progress++;
229                                                  [_indicator setDoubleValue:progress];                                                  [_indicator setDoubleValue:progress];
230                                          }                                          }
231                                  }                                  }
232                                  [image release];                                  [image release];
233                                    
234                          }                          }
235                            
236    //                      NSError *error;
237    //                      for (int i = 0; i < count; i++) {
238    //                              NSString* atPath = [dir stringByAppendingFormat:@"/%06d.jpg", i];
239    //                              NSString* toPath = [self._dirPath stringByAppendingFormat:@"/a%06d.jpg", i];
240    //                              if (![[NSFileManager defaultManager] moveItemAtPath:atPath toPath:toPath error:&error]) {
241    //                                      
242    //                                      NSRunAlertPanel(@"info", error.debugDescription,
243    //                                                                      nil, nil, nil);
244    //                                      break;
245    //                              }
246    //                      }
247                            
248                            if (0 != zipFlag) {
249                                    NSString* zipFile = self._dirPath;
250                                    ZipArchive* archiver = [[ZipArchive alloc] init];
251                                    [archiver CreateZipFile2:zipFile];
252                                    
253                                    for (int i = 0; i < count; i++) {
254                                            NSString* atPath = [dir stringByAppendingFormat:@"/%06d.%@", i, [self imageTypeExp:imageType]];
255                                            NSString* toPath = [NSString stringWithFormat:@"%06d.%@", i, [self imageTypeExp:imageType]];
256                                            [archiver addFileToZip:atPath
257                                                                       newname:toPath];
258                                    }
259                                    [archiver CloseZipFile2];
260                                    [archiver release];
261                                    
262                                    for (int i = 0; i < count; i++) {
263                                            NSError *error;
264                                            NSString* atPath = [dir stringByAppendingFormat:@"/%06d.%@", i, [self imageTypeExp:imageType]];
265                                            
266                                            if (![[NSFileManager defaultManager] removeItemAtPath:atPath error:&error]) {
267                                                    NSLog(@"error %@", error.debugDescription);
268                                            }
269                                    }
270                            }
271                            
272                          NSRunAlertPanel(@"info", @"finish",                          NSRunAlertPanel(@"info", @"finish",
273                                                          nil, nil, nil);                                                          nil, nil, nil);
274                            
275                            
276                            
277                  }                  }
278          } else {          } else {
279                  NSRunAlertPanel(@"info", @"set a folder",                  NSRunAlertPanel(@"info", @"set a folder",
# Line 174  NSImage* trimImageByRect(NSImage *image, Line 281  NSImage* trimImageByRect(NSImage *image,
281          }          }
282  }  }
283    
284  - (IBAction)onSelectSaveAs:(id)sender  - (IBAction)onClearButton:(id)sender
285  {  {
286      NSInteger result;          [mainView._files removeAllObjects];
287      NSOpenPanel* openPanel;          _dataArray = [[NSMutableArray alloc] init];
288            [_tableView reloadData];
289    }
290    
291    - (IBAction)onOpenButton:(id)sender
292    {
293            NSInteger result;
294            NSOpenPanel* openPanel;
295                    
296      openPanel = [NSOpenPanel openPanel];          openPanel = [NSOpenPanel openPanel];
297      [openPanel setCanChooseDirectories:YES];          [openPanel setCanChooseDirectories:YES];
298          [openPanel setCanCreateDirectories:YES];          [openPanel setCanCreateDirectories:YES];
299      result = [openPanel runModal];          [openPanel setAllowsMultipleSelection:YES];
300      if(result == NSOKButton) {          
301                  NSString*       dirPath = [[openPanel URL] path];          result = [openPanel runModal];
302                  BOOL isDirectory;          if(result == NSOKButton) {
303                  if ([[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&isDirectory]) {                  
304                          if (isDirectory) {                  
305                                  self._dirPath = dirPath;                  for (NSURL *url in [openPanel URLs]) {
306                                  [_textField setStringValue:dirPath];                          NSString *path = [url path];
307                          } else {                          
308                                  NSRunAlertPanel(@"info", @"set a folder",                          BOOL isDirectory;
309                                                                  nil, nil, nil);                          if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
310                                    if (isDirectory) {
311                                            NSArray* array = [[NSFileManager defaultManager] subpathsAtPath:path];
312                                            array = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
313                                            
314                                            for (NSString *filename in array) {
315                                                    filename = [path stringByAppendingPathComponent:filename];
316                                                    if ([NSImage isImageFile:filename]) {
317                                                            NSLog(@"%@", filename);
318                                                            [mainView._files addObject:filename];
319                                                    }
320                                            }
321                                    } else {
322                                            if ([NSImage isImageFile:path]) {
323                                                    [mainView._files addObject:path];
324                                            }
325                                    }
326                                    
327                          }                          }
328                  }                  }
329          }          }
330  }          
331            NSArray *files = mainView._files;
332  - (IBAction)onClearButton:(id)sender          
 {  
         [mainView._files removeAllObjects];  
333          _dataArray = [[NSMutableArray alloc] init];          _dataArray = [[NSMutableArray alloc] init];
334            for (int i = 0; i < [files count]; i++) {
335                    NSDictionary *data = @{@"title": [files[i] lastPathComponent],
336                                                     @"description": files[i]};
337                    [_dataArray addObject:data];
338            }
339          [_tableView reloadData];          [_tableView reloadData];
340  }  }
341    
342    - (IBAction)onInfo:(id)sender {
343            if (0 < [mainView._files count] && 0 <= [_tableView selectedRow]) {
344                    NSString *selectedStringValue = [mainView._files objectAtIndex:[_tableView selectedRow]];
345                    if (selectedStringValue) {
346                            if ([[NSWorkspace sharedWorkspace] openFile:selectedStringValue]) { //withApplication
347                                    return;
348                            }
349                    }
350            }
351            NSRunAlertPanel(@"info", @"can't open",
352                                            nil, nil, nil);
353    }
354    
355    //- (IBAction)savePDF:(id)sender
356    //{
357    //      // (1) setup filename
358    //      NSArray *paths = NSSearchPathForDirectoriesInDomains(
359    //                                                                                                               NSDesktopDirectory, NSUserDomainMask, YES);
360    //      NSString* path = [NSString stringWithFormat:@"%@/test.pdf",
361    //                                        [paths objectAtIndex:0], nil];
362    //      
363    //      // (2) setup WebView
364    //      NSView* view = [[[_web_view mainFrame] frameView] documentView];
365    //      
366    //      // (3) setup NSDictionary for NSPrintInfo
367    //      NSMutableDictionary* p_dict = [NSMutableDictionary
368    //                                                                 dictionaryWithDictionary:[[NSPrintInfo sharedPrintInfo] dictionary]];
369    //      [p_dict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
370    //      [p_dict setObject:path forKey:NSPrintSavePath];
371    //      
372    //      // (4) setup NSPrintInfo
373    //      NSPrintInfo* p_info = [[NSPrintInfo alloc] initWithDictionary:p_dict];
374    //      [p_info setHorizontalPagination:NSAutoPagination];
375    //      [p_info setVerticalPagination:NSAutoPagination];
376    //      [p_info setVerticallyCentered:NO];
377    //      
378    //      // (5) setup NSPrintOperation
379    //      NSPrintOperation* p_ope =
380    //      [NSPrintOperation printOperationWithView:view
381    //                                                                 printInfo:p_info];
382    //      [p_ope setShowsPrintPanel:NO];
383    //      
384    //      // (6) print it
385    //      [p_ope runOperation];
386    //}
387    
388  @end  @end

Legend:
Removed from v.1  
changed lines
  Added in v.2

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26