com.polyvi.xface.extension.capture.XCaptureScreenImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.polyvi.xface.extension.capture.XCaptureScreenImpl.java

Source

/*
 This file was modified from or inspired by Apache Cordova.
    
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License. You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied. See the License for the
 specific language governing permissions and limitations
 under the License.
*/

package com.polyvi.xface.extension.capture;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Rect;
import android.view.View;

import com.polyvi.xface.extension.XCallbackContext;
import com.polyvi.xface.util.XBase64;
import com.polyvi.xface.util.XFileUtils;
import com.polyvi.xface.util.XLog;
import com.polyvi.xface.util.XPathResolver;
import com.polyvi.xface.util.XStringUtils;

public class XCaptureScreenImpl {
    private static final String CLASS_NAME = XCaptureScreenImpl.class.getSimpleName();
    private static final String DATE_FORMAT_TYPE = "yyyyMMdd_HHmmss";
    private static final String TAG_DEFAULT_IMAGE_TYPE = ".jpeg";
    private static final String MIME_PNG_TYPE = "image/png";
    private static final String MIME_JPEG_TYPE = "image/jpeg";
    private static final int HIGHQUALITY = 100;
    /**json*/
    private static final String TAG_CODE = "code";
    private static final String TAG_RESULT = "result";

    /**??*/
    public static enum ResultCode {
        SUCCESS, ARGUMENT_ERROR, IO_ERROR,
    };

    private Activity mActivity;
    private XCallbackContext mCallbackContext;
    private String mWorkspace;
    private XCaptureScreenOptions mOptions;
    private View mView;

    public XCaptureScreenImpl(Activity activity, XCallbackContext callbackContext, String workspace,
            XCaptureScreenOptions options, View view) {
        mActivity = activity;
        mCallbackContext = callbackContext;
        mWorkspace = workspace;
        mOptions = options;
        mView = view;
    }

    /**
     * ?
     */
    public void startCaptureScreen() {
        if (null == mView) {
            mCallbackContext.error(getResult(ResultCode.ARGUMENT_ERROR, "argument is invalid"));
            ;
        }
        mActivity.runOnUiThread(new Runnable() {
            public void run() {
                mView.setDrawingCacheEnabled(true);
                mView.buildDrawingCache();
                Bitmap bitmap = mView.getDrawingCache();
                onCaptured(bitmap);
            }
        });
    }

    private void onCaptured(Bitmap originBitmap) {
        try {
            /**??*/
            Bitmap bitmap = getBitmap(originBitmap);
            if (null == bitmap) {
                mCallbackContext.error(getResult(ResultCode.ARGUMENT_ERROR, "argument is invalid"));
                return;
            }
            /**??MIMEType*/
            String path = getResolvePath(mOptions.getDestionationFile(), mWorkspace);
            if (XStringUtils.isEmptyString(path)) {
                mCallbackContext.error(getResult(ResultCode.IO_ERROR, "resolve path error"));
                return;
            }
            CompressFormat type = getMimeType(path);
            if (null == type) {
                mCallbackContext.error(getResult(ResultCode.ARGUMENT_ERROR, "argument is invalid"));
                return;
            }
            /** ? */
            int destinationType = mOptions.getDestinationType();
            if (destinationType == XCaptureScreenOptions.getTagFileUri()) {
                getFileUri(bitmap, path, type);
            } else {
                getDataUrl(bitmap, type);
            }
        } finally {
            mView.destroyDrawingCache();
        }
    }

    /**
     * ???
     * @param path:?
     */
    private CompressFormat getMimeType(String path) {
        String mimeType = XFileUtils.getMimeType(path);
        CompressFormat type = Bitmap.CompressFormat.JPEG;
        if (mimeType.equalsIgnoreCase(MIME_PNG_TYPE)) {
            type = Bitmap.CompressFormat.PNG;
        } else if (mimeType.equalsIgnoreCase(MIME_JPEG_TYPE)) {
            type = Bitmap.CompressFormat.JPEG;
        } else {
            return null;
        }
        return type;
    }

    /**
     * ??
     * @param originBitmap 
     * @return
     */
    private Bitmap getBitmap(Bitmap originBitmap) {
        if (null == originBitmap) {
            return null;
        }
        /** xy???? */
        if (mOptions.iSDefault()) {
            return originBitmap;
        }
        mOptions = getValidOptions(originBitmap);
        if (null == mOptions) {
            return null;
        }
        return Bitmap.createBitmap(originBitmap, mOptions.getX(), mOptions.getY(), mOptions.getWidth(),
                mOptions.getHeight());
    }

    /**
     * ?xy??widthheight
     */
    private XCaptureScreenOptions getValidOptions(Bitmap bitmap) {
        int captureRectX = mOptions.getX();
        int captureRectY = mOptions.getY();
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        int width = mOptions.getWidth();
        int height = mOptions.getHeight();
        Rect captureRect = new Rect(0, 0, 0, 0);
        /**?x y??xy*/
        if (mOptions.iSOnlyXAndYInput()) {
            captureRect = new Rect(captureRectX, captureRectY, bitmapWidth + captureRectX,
                    bitmapHeight + captureRectY);
        } else {
            captureRect = new Rect(captureRectX, captureRectY, width + captureRectX, height + captureRectY);
        }
        Rect bitmapRect = new Rect(0, 0, bitmapWidth, bitmapHeight);
        if (captureRect.intersect(bitmapRect)) {
            mOptions.setX(captureRect.left);
            mOptions.setY(captureRect.top);
            mOptions.setWidth(captureRect.right - captureRect.left);
            mOptions.setHeight(captureRect.bottom - captureRect.top);
            /** ???*/
            if (mOptions.getWidth() <= 0 || mOptions.getHeight() <= 0) {
                return null;
            }
            return mOptions;
        } else {
            return null;
        }
    }

    /**
     * ?,url?
     */
    private void getFileUri(Bitmap bitmap, String savePath, CompressFormat mimeType) {
        /** ?? */
        mkDir(savePath);
        File pic = new File(savePath);
        String absPath = pic.getAbsolutePath();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(pic);
            bitmap.compress(mimeType, HIGHQUALITY, fos);
            fos.flush();
            fos.close();
            mCallbackContext.success(getResult(ResultCode.SUCCESS, pic.getAbsolutePath()));
        } catch (FileNotFoundException e) {
            String msg = "File Not Found! Path is " + absPath;
            XLog.e(CLASS_NAME, msg);
            mCallbackContext.error(getResult(ResultCode.IO_ERROR, msg));
        } catch (IOException e) {
            String msg = e.getMessage();
            XLog.e(CLASS_NAME, msg);
            mCallbackContext.error(getResult(ResultCode.IO_ERROR, msg));
        }
    }

    /**
     * ?,?base64?
     */
    private void getDataUrl(Bitmap bitmap, CompressFormat mimeType) {
        ByteArrayOutputStream fos = null;
        try {
            fos = new ByteArrayOutputStream();
            bitmap.compress(mimeType, HIGHQUALITY, fos);
            byte[] result = fos.toByteArray();
            if (null != fos) {
                fos.close();
            }
            mCallbackContext
                    .success(getResult(ResultCode.SUCCESS, new String(XBase64.encode(result, XBase64.NO_WRAP))));
        } catch (IOException e) {
            String msg = "get data Exception:" + e.getMessage();
            XLog.e(CLASS_NAME, msg);
            mCallbackContext.error(getResult(ResultCode.IO_ERROR, msg));
        }
    }

    /**
     * ?
     *
     * @param savePath
     *            : ??
     * @param workSpace
     *            : 
     * @return ??
     */
    private String getResolvePath(String savePath, String workSpace) {
        savePath = XStringUtils.isEmptyString(savePath.replace(" ", "")) ? getFileName() : savePath;
        String resolveSavePath = new XPathResolver(savePath, workSpace).resolve();
        if (null == resolveSavePath) {
            return null;
        }
        /** sdcard???sdcard?null*/
        String sdcardPath = XFileUtils.getSdcardPath();
        if (null == sdcardPath && !XStringUtils.isEmptyString(savePath) && savePath.startsWith("file://sdcard")) {
            return null;
        }
        /**path??*/
        return isDirectory(resolveSavePath) ? new File(resolveSavePath, getFileName()).getAbsolutePath()
                : resolveSavePath;
    }

    /**
     * ???????.
     *
     * @param savePath
     *            ?
     * @return ??
     */
    private String getFileName() {
        return new SimpleDateFormat(DATE_FORMAT_TYPE).format(Calendar.getInstance().getTime())
                + TAG_DEFAULT_IMAGE_TYPE;
    }

    /**
     * 
     *
     * @param savePath ?
     * @return
     */
    private boolean mkDir(String savePath) {
        File dirFile = new File(savePath).getParentFile();
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return dirFile.mkdirs();
        }
        return true;
    }

    /**
     * json
     */
    private JSONObject getResult(ResultCode code, String result) {
        JSONObject obj = new JSONObject();
        try {
            obj.put(TAG_CODE, code.ordinal());
            obj.put(TAG_RESULT, result);
            return obj;
        } catch (JSONException e) {
            XLog.e(CLASS_NAME, e.getMessage());
        }
        return null;
    }

    /**
     * url?
     * url?
     * file://sdcard/test/sdcard.jpg, true
     * /test/workspace.jpg, true
     * file://sdcard/test/, false
     * file://sdcard/test, false
     * file://sdcard/test., false
     */
    private boolean isDirectory(String url) {
        /**?"/"?*/
        String fileName = new File(url).getName();
        if (XStringUtils.isEmptyString(fileName)) {
            return true;
        }
        int indexOfPoint = fileName.lastIndexOf(".");
        /**??url?*/
        if (indexOfPoint < 0 || indexOfPoint + 1 == fileName.length()) {
            return true;
        }
        return false;
    }
}