2017-12-12

Simple AngularJS Binding With Nested Components

Simple AngularJS Binding With Nested Components

This is a simple example of binding between two nested components.


<html>
<head>
</head>
<body ng-app="app">
<h1>Html</h1>
<parent>
</parent>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.component("parent", {
template : "<h1>Parent</h1><button ng-click='controller.show()'>Show</button><child></child>",
controllerAs : "controller",
binding : {
data : "="
},
controller : [function() {
var controller = this;
controller.show = function() {
alert(JSON.stringify(data));
};
}
]
});
app.component("child", {
template : "<h2>Child</h2><textarea ng-model='controller.model.stuff'></textarea>",
controllerAs : "controller",
binding : {
data : "="
},
controller : [function() {
var controller = this;
controller.model = {
stuff : "text"
};
data = controller.model;
}
]
});
</script>
</body>
</html>
view raw binding.html hosted with ❤ by GitHub

2017-12-10

Servlet Using Google Vision API Sample

Servlet Using Google Vision API

A simple servlet with POST getting an image URL and performs a SAFE SEARCH check using Google Vision API.
Note that it doesn't contain authorization. You can do it locally on your machine or run it live at App Engine using it's service account (it does that automatic so you just deploy it).

package checkpic
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.api.client.util.Lists;
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.Likelihood;
import com.google.cloud.vision.v1.SafeSearchAnnotation;
import com.google.common.io.ByteStreams;
import com.google.protobuf.ByteString;
/**
* Servlet with POST containing URL to an image. The image is downloaded and sent to
* Google Vision API and checks SAFE SEARCH (it's easy to select another check).
* Note: This does not contain local authorization to Vision API. It only runs
* live using the app's service account.
*/
@SuppressWarnings("serial")
@WebServlet(name = "Check Pic", value = "/check-pic/")
public class CheckPicServlet extends HttpServlet {
private static Logger log = Logger.getLogger(CheckPicServlet.class.getName());
@Override
public void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
String content = httpRequest.getReader().readLine();
String imageUrl = URLDecoder.decode(content, "UTF-8");
log.info("Checking:" + imageUrl);
URL url = new URL(imageUrl);
byte[] array = ByteStreams.toByteArray(url.openStream());
ImageAnnotatorClient vision = ImageAnnotatorClient.create();
List<AnnotateImageRequest> requests = Lists.newArrayList();
ByteString imgBytes = ByteString.copyFrom(array);
Image img = Image.newBuilder().setContent(imgBytes).build();
// Use SAFE SEARCH
Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
AnnotateImageRequest visionRequest = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();
requests.add(visionRequest);
// Performs label detection on the image file
BatchAnnotateImagesResponse visionResponse = vision.batchAnnotateImages(requests);
List<AnnotateImageResponse> annotationResponses = visionResponse.getResponsesList();
for (AnnotateImageResponse annotationResponse : annotationResponses) {
if (annotationResponse.hasError()) {
httpResponse.setStatus(400);
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
SafeSearchAnnotation annotation = annotationResponse.getSafeSearchAnnotation();
}
}
}