在android webview实现截屏的手动tounchmove裁剪图片
2012-08-29 11:26
633 查看
first,import phonegap.
js
java
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; } } }
相关文章推荐
- Android使用WebView实现图片点击的监听
- android困惑之UI---imageView实现对图片的选取,和裁剪功能。。。。
- Android实现图文混排(2) 通过webview实现 并实现点击 图片处理事件
- Android之UI学习篇七:ImageView实现适屏和裁剪图片的功能
- Android 中 WebView 与 js 简单交互实现图文混排效果,解决图片自适应屏幕与查看大图问题
- android 通过Camera和Surfaceview实现照相 图片预览 保存图片到本地 手动聚焦 FaceDetectionListener实现人脸识别
- Android实现点击WebView界面中图片滑动浏览与保存图片功能
- Android中WebView图片实现自适应的方法
- Android实现保存图片,长图、长布局(webView等)
- Android 图文混排 通过webview实现并实现点击图片
- Android 通过注入js实现读取webview中图片并放大的优化
- Android自定义 view之图片裁剪从设计到实现
- android webView使用js/css实现夜间模式 长按识别图片以及二维码,网页可以上传图片
- Android webview实现上传图片的效果(图片压缩)
- Android学习之实现WebView中input="file"选择文件,处理选择图片无法返回类型问题
- Android之UI学习篇七:ImageView实现适屏和裁剪图片的功能
- Android实现保存图片,长图、长布局(webView等)
- Android 中 WebView 与 js 简单交互实现图文混排效果,解决图片自适应屏幕与查看大图问题
- android中Webview实现截屏三种方式小结
- Android webview打开本地图片上传实现代码