如何在android studio中将图像视图转换为位图?

时间:2015-08-13 01:56:22

标签: android image bitmap

有谁能告诉我如何将imageview转换为位图?在我的应用程序中,我访问图库并将图片的位图获取到imageview,然后使用毕加索,我调整了太大而无法直接上传到解析的图像。我可以使用毕加索在图像视图中调整图像大小但是如何从图像视图中获取位图以上传到解析?这是我的代码..

connect: function(){
  var fulladdr = completeServerAddress(address);
  try {
    connection = new WebSocket(fulladdr);
    connection.suppressErrorsBecauseOfAutoConnection = suppressErrorsBecauseOfAutoConnection; //Store this module-scoped variable in connection, so if the module changes suppression state, this connection won't.
  } catch (e){
    //Make sure we don't try to send anything down this dead websocket
    connection = false;
    return false;
  }
  connection.binaryType = "arraybuffer";
  connection.onerror = function(){
    if (connection !== false && !connection.suppressErrorsBecauseOfAutoConnection){
      Announce.announceMessage("Connection failed with server");
    }
    connection = false;
  };
  connection.onmessage = function(m){
    rxMessage(ConnectionProtocol.BaseMessage.parseChunk(m.data));
  };
  connection.onclose = function(){
    hooks.swing("disconnected", "", 0);
    if (connection !== false && !connection.suppressErrorsBecauseOfAutoConnection){
      Announce.announceMessage("Connection lost with server");
    }
  };
  connection.onopen = function(){
    sendMessages(ConnectionProtocol.HandshakeMessage.create(name, sources, sinks));
    while (idlingmessages.length){
      websocketConnection.send(idlingmessages.splice(0,1)[0]);
    }
    hooks.swing("connected", "", 0);
  };
},

1 个答案:

答案 0 :(得分:0)

您只能从imageview生成位图,但不能以实际图像大小生成位图,这会生成具有imageview大小的位图

Bitmap bitmap;
            try {
                bitmap = Bitmap.createBitmap(YOUR_VIEW.getWidth(), YOUR_VIEW.getHeight(),
                        Bitmap.Config.ARGB_8888);

                Canvas canvas = new Canvas(bitmap);
                YOUR_VIEW.draw(canvas);

                File root = Environment.getExternalStoragePublicDirectory(Environment.PICTURES);

                String fname = "NAME_OF_FILE.jpg";
                file = new File(root, fname);

                try {
                    if (!root.exists()) {
                    root.mkdir();
                }
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    out.close();
                    YOUR_VIEW.destroyDrawingCache();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
            }

如果您不想保存到文件,请使用以下代码:

public static Bitmap getScreenViewBitmap(View v) {
    v.setDrawingCacheEnabled(true);

    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will be null
    v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false); // clear drawing cache

    return b;
}