安卓:显示在img标签捕获图像 [英] Android: Display Captured image in img tag

查看:361
本文介绍了安卓:显示在img标签捕获图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML页面IMG标记显示拍摄的图像。我可以通过 data.getData()从OnActivityResult事件得到URI值。但我不知道inwhich格式,我可以发送图像和img标签来显示。请人帮忙米

I want to display captured image in img tag in html page. I can get the URI value by data.getData() from the OnActivityResult event. But I dont know inwhich format i can send the image and to display in img tag. Please anyone help m

Camera.java:

Camera.java:

  public class Camera extends Activity 
    {
         private static final int CAMERA_REQUEST = 1888;
         private String selectedImagePath;
         WebView webview;
         String fileName = "capturedImage.jpg";
         private static Uri mCapturedImageURI; 

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
                webview=(WebView)findViewById(R.id.webView1);
        }

        public void TakePhoto()
        {   
                ContentValues values = new ContentValues();  
                values.put(MediaStore.Images.Media.TITLE, fileName);  
                mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }       
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (resultCode == RESULT_OK)
                {
                  if (requestCode == CAMERA_REQUEST) 
                  { 
                    selectedImagePath = getPath(mCapturedImageURI);
                webview.loadUrl("javascript:ReceivePhoto(\""+selectedImagePath+"\")");
                  }
                }
        }

        public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

    }

HTML:

Html:

 function CallBack(resultimage) 
 {
       myimg.src = resultimage;
 } 

在拍摄照片的活动将被重新装入和放大器;同时启动我的应用程序,它显示等待调试......。我做了错误?任何人都可以请帮我...

After taking the photo activity is reloaded & while starting my application, it shows "Waiting for Debugging....". What mistake i did? can anyone please help me...

推荐答案

有一个解决方案。 : - )

Got a Solution. :-)

public class Camera extends Activity 
{
     private static final int CAMERA_REQUEST = 1888;
     private String selectedImagePath;
     WebView webview;
     String fileName = "capturedImage.jpg";
     FileOutputStream fo;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
            webview=(WebView)findViewById(R.id.webView1);
    }

    public void TakePhoto()
    {   

            Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }       
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == RESULT_OK)
            {
              if (requestCode == CAMERA_REQUEST) 
              { 
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
            Random randomGenerator = new Random();randomGenerator.nextInt(100);
            String newimagename=randomGenerator.toString()+".jpg";
            File f = new File(Environment.getExternalStorageDirectory()
                                    + File.separator + newimagename);
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //write the bytes in file

            try {
                fo = new FileOutputStream(f.getAbsoluteFile());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            webview.loadUrl("javascript:ReceivePhoto(\""+f.getAbsolutePath()+"\")");
              }
            }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

}

ManifestFile:

ManifestFile:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这篇关于安卓:显示在img标签捕获图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆