- first,import phonegap.
- js
-
var isSelect = false;
var w, h;
function capturePhoto(){
var mask = $("<div></div>");
mask.css({
'filter': 'alpha(opacity=80)',
'opacity': 0.5,
'background-color': 'gray',
'width': '100%',
'height': '100%',
'position': 'fixed',
'left': 0,
'top': 0,
'z-index': 99998,
'text-align': 'center',
'vertical-align': 'middle'
});
area = $("<div></div>").css({
'display': 'none'
});
$(document.body).append(area);
$(document.body).append(mask);
mask.bind({
"touchstart mousedown": function(e){
e.preventDefault();
// Handle the start
var event = e.originalEvent, touch = event.targetTouches ? event.targetTouches[0] : e;
xStart = touch.pageX;
yStart = touch.pageY;
},
"touchend mouseup": function(e){
// Handle the end
e.preventDefault();
var event = e.originalEvent;
var touch = event.changedTouches ? event.changedTouches[0] : e;
w = touch.pageX - xStart;
h = touch.pageY - yStart;
if (isSelect) {
if (window.confirm("are you sure save?")) {
mask.css({
"display": "none"
});
alert(window.MyCls.capturePicture(xStart, yStart, w, h));
}
else {
isSelect = false;
}
}
area.fadeOut();
},
"touchmove": function(e){
e.preventDefault();
var event = e.originalEvent;
var touch = event.changedTouches ? event.changedTouches[0] : e;
var width = touch.pageX - xStart;
var height = touch.pageY - yStart;
if (width > 0 && height > 0) {
isSelect = true;
}
area.css({
'background-color': '#fff',
'opacity': 0.1,
'width': width + "px",
'height': height + "px",
'position': 'fixed',
'left': xStart + "px",
'top': yStart + "px",
'z-index': 99999,
'display': 'none'
}).show();
}
});
}
- java
package com.sangeco.plant;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.os.Bundle;
import android.webkit.WebView;
import com.phonegap.DroidGap;
public class MainActivity extends DroidGap {
/** Called when the activity is first created. */
private JavaScriptInterface jsGap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
// WebSettings ws = super.appView.getSettings();
jsGap = new JavaScriptInterface(this, appView);
appView.addJavascriptInterface(jsGap, "MyCls");
super.loadUrl("file:///android_asset/html/index.html");
}
public class JavaScriptInterface {
private WebView mAppView;
private DroidGap mGap;
public JavaScriptInterface(DroidGap gap, WebView view) {
mAppView = view;
mGap = gap;
}
public String getTelephoneNumber(String msg) {
return msg;
}
public String capturePicture(String x,String y,String w,String h) {
int left = Integer.parseInt(x);
int top = Integer.parseInt(y);
int width = Integer.parseInt(w);
int height = Integer.parseInt(h);
String path = "photo saved as ";
Bitmap bitmap2=Bitmap.createBitmap(getScreenShot(), left, top, width, height);
try {
String filename=System.currentTimeMillis()+".jpg";
path += saveImage(filename, bitmap2)+"\n";
} catch (IOException e) {
path=e.getLocalizedMessage();
e.printStackTrace();
}
return path;
}
public String cutScreen() {
String path = "photo saved as ";
Picture picture = mAppView.capturePicture();
Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
try {
String filename = System.currentTimeMillis() + ".jpg";
path += saveImage(filename, bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
path = e.getLocalizedMessage();
e.printStackTrace();
}
return path;
}
private Bitmap getScreenShot(){
Picture picture = mAppView.capturePicture();
Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
return bitmap;
}
private String saveImage(String filename, Bitmap bitmap) throws IOException {
String sdState = android.os.Environment.getExternalStorageState();
String img = null;
if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
File SDFile = android.os.Environment.getExternalStorageDirectory();
img=SDFile.getAbsolutePath() + File.separator
+ filename;
File myFile = new File(img);
if (!myFile.exists()) {
myFile.createNewFile();
}
BufferedOutputStream os = new BufferedOutputStream(
new FileOutputStream(myFile));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
}
return img;
}
}
}