zoukankan      html  css  js  c++  java
  • 在android webview实现截屏的手动tounchmove裁剪图片

    1. first,import phonegap.
    2. js
      1. 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();
        }
        });

        }


    3. 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;
    }

    }
    }
  • 相关阅读:
    简体繁体互译代码段:
    require.js使用baseUrl + paths导入文件配置的3种方法
    vue + vue-router + vue-resource 基于vue-cli脚手架 --->笔记
    解决webstorm卡顿问题
    js 函数闭包内部返回函数体调用方法难点解答
    java学习笔记之位运算符
    java集合类学习笔记之LinkList
    java集合类学习笔记之ArrayList
    java学习笔记之对象序列化
    springboot集成巨杉数据库
  • 原文地址:https://www.cnblogs.com/tuolin/p/2375712.html
Copyright © 2011-2022 走看看