Skip to content

Commit

Permalink
Merge branch 'removed_thumbs'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mevin Dhunnooa committed Aug 3, 2018
2 parents cc3cbb4 + a560a9d commit b723745
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 102 deletions.
54 changes: 1 addition & 53 deletions src/android/GeniusScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,58 +87,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return false;
}

private void generateThumbnail(Context context, String pathOfInputImage){
try
{
int inWidth = 0;
int inHeight = 0;
int dstWidth = 370;
int dstHeight = 370;

InputStream in = new FileInputStream(pathOfInputImage);

// decode image size (decode metadata only, not the whole image)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
in.close();
in = null;

// save width and height
inWidth = options.outWidth;
inHeight = options.outHeight;

// decode full image pre-resized
in = new FileInputStream(pathOfInputImage);
options = new BitmapFactory.Options();
// calc rough re-size (this is no exact resize)
options.inSampleSize = Math.max(inWidth/dstWidth, inHeight/dstHeight);
// decode full image
Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

// calc exact destination size
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
float[] values = new float[9];
m.getValues(values);

// resize bitmap
Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);

// save image
File file = new File(pathOfInputImage);
String filename = file.getName();
FileOutputStream out = new FileOutputStream(new File(context.getFilesDir(), "thumb_" + filename).getAbsolutePath());
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);

}
catch (Exception e)
{
Log.e("Image", e.getMessage(), e);
}
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Expand All @@ -147,7 +96,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
if( resultCode == Activity.RESULT_OK)
{
String enhancedImageFilePath = scanContainer.getEnhancedImage().getAbsolutePath(null);
generateThumbnail(this.cordova.getActivity().getApplicationContext(), enhancedImageFilePath);
Uri enhancedImageFileUri = Uri.fromFile(new File(enhancedImageFilePath));
PluginResult result = new PluginResult(PluginResult.Status.OK, enhancedImageFileUri.toString());
result.setKeepCallback(true);
Expand Down
49 changes: 0 additions & 49 deletions src/ios/Scan.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,6 @@ - (void) rotateRight {
self.rotation += M_PI_2;
}

- (UIImage*)imageByScalingNotCroppingForSize:(UIImage*)anImage toSize:(CGSize)frameSize
{
UIImage* sourceImage = anImage;
UIImage* newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = frameSize.width;
CGFloat targetHeight = frameSize.height;
CGFloat scaleFactor = 0.0;
CGSize scaledSize = frameSize;

if (CGSizeEqualToSize(imageSize, frameSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;

// opposite comparison to imageByScalingAndCroppingForSize in order to contain the image within the given bounds
if (widthFactor == 0.0) {
scaleFactor = heightFactor;
} else if (heightFactor == 0.0) {
scaleFactor = widthFactor;
} else if (widthFactor > heightFactor) {
scaleFactor = heightFactor; // scale to fit height
} else {
scaleFactor = widthFactor; // scale to fit width
}
scaledSize = CGSizeMake(floor(width * scaleFactor), floor(height * scaleFactor));
}

UIGraphicsBeginImageContextWithOptions(scaledSize, YES, 1.0); // this will resize

[sourceImage drawInRect:CGRectMake(0, 0, scaledSize.width, scaledSize.height)];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if (newImage == nil) {
NSLog(@"could not scale image");
}

// pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}

- (void)saveAndSendCallback {
NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
Expand All @@ -81,12 +38,6 @@ - (void)saveAndSendCallback {

[UIImageJPEGRepresentation(rotatedImage, 0.85) writeToFile:enhancedImagePath atomically:YES];

//save also the thumbnail
UIImage* scaledImage = [self imageByScalingNotCroppingForSize:rotatedImage toSize:CGSizeMake(370, 370)];
NSString*thumbName= [NSString stringWithFormat:@"thumb_%@", [enhancedImagePath lastPathComponent]];
NSString*thumb=[enhancedImagePath stringByReplacingOccurrencesOfString:[enhancedImagePath lastPathComponent] withString:thumbName];
[UIImageJPEGRepresentation(scaledImage, 0.85) writeToFile:thumb atomically:YES];

NSString *resultFileUri = [Scan convertFilePathToFileUri:enhancedImagePath];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:resultFileUri];
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];
Expand Down

0 comments on commit b723745

Please sign in to comment.