Thursday, April 7, 2016

Go: imagick image croping after rotation problem

I need to load image rotate it by some angle, then crop some part of it. Seems trivial, isn't?

I use: gopkg.in/gographics/imagick.v2/imagick package.

Load image
iwand := imagick.NewMagickWand()
defer iwand.Destroy()
if err := iwand.ReadImage(s); err != nil {
 log.Panicf("cannot open image %s", err)
}
Rotate it, fill new area with yellow
w := iwand.GetImageWidth()
h := iwand.GetImageHeight()
log.Printf("old size: %d,%d", w, h)
pwand := imagick.NewPixelWand()
pwand.SetColor("yellow")
if err := iwand.RotateImage(pwand, 45); err != nil {
 log.Panicf("problem with rotation: %s", err)
}
Calculate the position and size of the rectangle to cut
newW := calcSize(w, h)
newH := newW

w = iwand.GetImageWidth()
h = iwand.GetImageHeight()
log.Printf("new size: %d,%d", w, h)

x := int((w - newW) / 2)
y := int((h - newH) / 2)
Make the area red (with transparency), just for learning
dwand := imagick.NewDrawingWand()
pwand.SetColor("red")
pwand.SetOpacity(0.5)
dwand.SetFillColor(pwand)
dwand.Rectangle(float64(x), float64(y), float64(newW+uint(x)), float64(newH+uint(y)))
iwand.DrawImage(dwand)
Crop and save to file
log.Printf("w,h,x,y: %d,%d,%d,%d", newW, newH, x, y)
if err := iwand.CropImage(newW, newH, x, y); err != nil {
 log.Fatalf("problem with crop: %s", err)
}
iwand.WriteImage(d)

Input file
Original image (from: https://en.wikipedia.org/wiki/Linux)

Output file
Image rotated and cropped

Need to repage!
log.Printf("w,h,x,y: %d,%d,%d,%d", newW, newH, x, y)
iwand.ResetImagePage("")
if err := iwand.CropImage(newW, newH, x, y); err != nil {
 log.Fatalf("problem with crop: %s", err)
}
iwand.WriteImage(d)
Rotated and cropped - fixed
Full source: gist

Friday, March 11, 2016

Android developer road

So, I'm going through a quick Android developer course. The main target is to learn how to provide the best Android apps with minimal effort. Assumption is we all know Java ;-)

The start

A good starting point is:
http://developer.android.com/training/index.html

I don't recommend to read it all at the beginning. I would suggest:
Others as needed, Best Practices for Interaction and Engagement (http://developer.android.com/training/best-ux.html) also helps.

The above gives you a rough overview. The real stuff comes in API Guides (http://developer.android.com/guide/components/index.html). I do recommend to read the first three sections:
This gives you a high level overview of how the Android works.

The lifecycle

You basically should have it at your desk. You will use it often. It's been reported to have some inaccuracies but it's still better than the Google provided simplified version.



Codelabs

Google provides two interesting codelabs:
If you have to provide some early results quickly, skip above readings and just do the codelab.

Other must-to-know

Butterknife - http://jakewharton.github.io/butterknife/ - saves you lot of boilerplate with binding view elements to variables.
Android ContentProvider Generator - it will save you a lot of time on boilerplate code - https://github.com/BoD/android-contentprovider-generator
Mosby - http://hannesdorfmann.com/mosby - if you go with MVP this is must to use.
Dagger - dependency injection framework - http://google.github.io/dagger/users-guide

And when you consider which Android version to support, check:
Android stats Dashboard - gives an overview of the Android version distribution - http://developer.android.com/about/dashboards/index.html

Learning

Monday, February 8, 2016

No, because no

I need Math.Round from Go's standard library, poor me:


I know it's hard. But hey, most languages provide it.

Rob Pike:
Floating point numbers don't have digits.
The bar for being useful needs to be pretty high to be in the Go math package. We accepted most of what's in the C library but we're not going to take all the routines from other libraries. The Javascript round is a 1-liner: int(f+0.5). 

Wednesday, January 27, 2016

Angular-2.0.0.beta1

Http problem

I was getting an error:
error TS2305: Module '"angular2/http"' has no exported member 'HTTP_PROVIDERS'
may be caused by simply outdated angular2.d.ts installed through tsd.
DefinitelyTyped.org typings for Angular2 are outdated, tsd install angular2 gives old .d.ts which makes typescript unhappy. What’s the new approach or which angular.d.ts is appropriate?
When appropriate angular2 node package is installed everything works fine.

Duplicate properties

The other error I’ve encounter (Angular-2.0.0.beta.1):
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(6,14): error TS2300: Duplicate identifier 'PropertyKey'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(6,14): error TS2300: Duplicate identifier 'PropertyKey'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(9,5): error TS2300: Duplicate identifier 'done'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(10,5): error TS2300: Duplicate identifier 'value'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(248,5): error TS2300: Duplicate identifier 'EPSILON'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(283,5): error TS2300: Duplicate identifier 'MAX_SAFE_INTEGER'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(290,5): error TS2300: Duplicate identifier 'MIN_SAFE_INTEGER'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(346,5): error TS2300: Duplicate identifier 'flags'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(498,5): error TS2300: Duplicate identifier 'prototype'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(561,5): error TS2300: Duplicate identifier 'size'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(570,5): error TS2300: Duplicate identifier 'prototype'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(581,5): error TS2300: Duplicate identifier 'size'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(590,5): error TS2300: Duplicate identifier 'prototype'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(605,5): error TS2300: Duplicate identifier 'prototype'.
ts/node_modules/angular2/typings/es6-shim/es6-shim.d.ts(619,5): error TS2300: Duplicate identifier 'prototype'.
ts/typings/es6-shim/es6-shim.d.ts(6,14): error TS2300: Duplicate identifier 'PropertyKey'.
ts/typings/es6-shim/es6-shim.d.ts(9,5): error TS2300: Duplicate identifier 'done'.
ts/typings/es6-shim/es6-shim.d.ts(10,5): error TS2300: Duplicate identifier 'value'.
ts/typings/es6-shim/es6-shim.d.ts(248,5): error TS2300: Duplicate identifier 'EPSILON'.
ts/typings/es6-shim/es6-shim.d.ts(283,5): error TS2300: Duplicate identifier 'MAX_SAFE_INTEGER'.
ts/typings/es6-shim/es6-shim.d.ts(290,5): error TS2300: Duplicate identifier 'MIN_SAFE_INTEGER'.
ts/typings/es6-shim/es6-shim.d.ts(346,5): error TS2300: Duplicate identifier 'flags'.
ts/typings/es6-shim/es6-shim.d.ts(498,5): error TS2300: Duplicate identifier 'prototype'.
ts/typings/es6-shim/es6-shim.d.ts(561,5): error TS2300: Duplicate identifier 'size'.
ts/typings/es6-shim/es6-shim.d.ts(570,5): error TS2300: Duplicate identifier 'prototype'.
ts/typings/es6-shim/es6-shim.d.ts(581,5): error TS2300: Duplicate identifier 'size'.
ts/typings/es6-shim/es6-shim.d.ts(590,5): error TS2300: Duplicate identifier 'prototype'.
ts/typings/es6-shim/es6-shim.d.ts(605,5): error TS2300: Duplicate identifier 'prototype'.
ts/typings/es6-shim/es6-shim.d.ts(619,5): error TS2300: Duplicate identifier 'prototype'.

I simply solved it by specifying the files in tsconfig.json explicitly through files. A better solution is to remove typings/es6-shim/.

Thursday, December 17, 2015

Google Identity Toolkit for Android - weird compilation error

I’m hacking tiny Android app, one of the libraries I use is Google Identity Toolkit (it’s super easy to integrate). A documentation states that a Play Services should be added as dependency:
compile 'com.google.android.gms:play-services:8.3.0'
For any non trivial application it’s going to be a problem resulting in below error (or similar):
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 2
Search on Internet points to StackOverflow question: com.android.build.transform.api.TransformException. Apparently the bugs are related. The exit return code 2 is meaningless unless one has any clues. Anyway, the problem can be solved by replacing the dependency by:
compile 'com.google.android.gms:play-services-plus:8.3.0'
compile 'com.google.android.gms:play-services-auth:8.3.0'
I’ve opened an issue on Google Identity Toolkit - Android sample project asking for more documentation.
The error is related to a constraint of Android Dalvik runtime of to 65K Reference Limit. There are two solutions for the problem: decreasing the references (usually by chosing smaller dependencies, in our case subpackages instead of the full Play Services) and using so called multidex (some 3rd party blog).