Android上 用Html5做介面,javascript調用網路攝影機執行個體

來源:互聯網
上載者:User

Android開發時候,介面製作基本會耗去大半部分時間,而且只能開發人員自己製作,所以一直想借用HTML5+CSS3完成,PhoneGap還不太成熟,自己準備用Android原生+WebView完成,發現和Flash As做互調差不多。以下代碼是通過HTML的javascript調用原生介面,完成照片選取的代碼,點擊映像,將選擇圖片(從相機或者網路攝影機)。照片選取代碼見上一篇文章 《Android 拍照加剪下處理方式

Activity代碼:

import sh.path.R;
import sh.path.ui.image.ImageCrop;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Toast;

public class HTMLFeedHomeActivity extends Activity {
    private ImageCrop photoUtil;
    private WebView webView = null;
    private Handler mHandler = new Handler();
    private String mJsData = null;
    private Activity activityThis = this;

    @Override
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.htmlfeeds);
        setupWebView();
        photoUtil = new ImageCrop(this);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }

    private void setupWebView() {

        webView = (WebView) findViewById(R.id.idhtmlfeedhome);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl("file:///android_asset/path/index.html");
        webView.addJavascriptInterface(new JavaScriptInterface(), "caller");
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try{
            if(photoUtil.capituredImage(requestCode, resultCode, data)){
                if(ImageCrop.CROP_IMAGE ==requestCode){
                    //mImageView.setImageURI(photoUtil.getImageCaptureUriCutted());
                    webView.loadUrl("javascript:setPhoto('"+mJsData+"','"+photoUtil.getImageCaptureUriCutted().getPath()+"')");
                }
                return;
            }
        }catch(Exception exp){
            exp.printStackTrace();
        }
    }
   
    private class JavaScriptInterface{
        @SuppressWarnings("unused")
        public void clickOnAndroid() {
            mHandler.post(new Runnable() {
                public void run() {
                    webView.loadUrl("javascript:wave()");
                }
            });
        }
        @SuppressWarnings("unused")
        public void takePhoto(String jsData){
            mJsData = jsData;
            photoUtil.getDialog().show();
        }
        @SuppressWarnings("unused")
        public void showTip(String tip){
            Toast.makeText(activityThis, tip, Toast.LENGTH_SHORT).show();  
        }
    }
}

詳見 《用Html5做介面,javascript調用網路攝影機執行個體》

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.