Read the canvas data and save it as a binary byte array to image data variable.
Get this data using the method getImageData().
@param canvas
339600
function(canvas) {
            var base64 = canvas.toDataURL('image/jpeg', options.jpegQuality)
                .replace(/^data:image\/(png|jpeg|jpg|gif);base64,/, '');
            imageData = base64toBlob(base64, 'image/jpeg');       // Byte array
        }
721571
public static BufferedImage read(URL imageUrl) {
		try {
			return ImageIO.read(imageUrl);
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}
	}
489679
public final ImageData copy()
    {
        final Context2D context = new ScratchPad(getWidth(), getHeight()).getContext();

        context.putImageData(this, 0, 0);

        return context.getImageData(0, 0, getWidth(), getHeight());
    }
816024
function getImageData(img) {
  let data = null;
  return new Promise((reslove, reject) => {
    if (img.src) {
      if (/^data\:/i.test(img.src)) { // Data URI
        data = base64ToArrayBuffer(img.src);
        reslove(data)
      } else if (/^blob\:/i.test(img.src)) { // Object URL
        var fileReader = new FileReader();
        fileReader.onload = function (e) {
          data = e.target.result;
          reslove(data)
        };
        objectURLToBlob(img.src, function (blob) {
          fileReader.readAsArrayBuffer(blob);
        });
      } else {
        var http = new XMLHttpRequest();
        http.onload = function () {
          if (this.status == 200 || this.status === 0) {
            data = http.response
            reslove(data)
          } else {
            throw "Could not load image";
          }
          http = null;
        };
        http.open("GET", img.src, true);
        http.responseType = "arraybuffer";
        http.send(null);
      }
    } else {
      reject('img error')
    }
  })
}
388630
function drawToCanvas({data, width, height}) {
  if (typeof document === 'undefined') return null

    var canvas = document.createElement('canvas')
    canvas.width = width
    canvas.height = height
    var context = canvas.getContext('2d')
    var idata = context.createImageData(canvas.width, canvas.height)
    for (var i = 0; i < data.length; i++) {
      idata.data[i] = data[i]
    }
    context.putImageData(idata, 0, 0)
    return canvas
}
790163
public byte[] drawTileData(short[][] pixelValues) {
		BufferedImage image = drawTile(pixelValues);
		byte[] bytes = getImageBytes(image);
		return bytes;
	}
874181
public function getImageDataUri($string, $size = 64, $color = null)
	{
		return sprintf('data:image/png;base64,%s', base64_encode($this->getImageData($string, $size, $color)));
	}
345662
public static BufferedImage readQuietly(final byte[] byteArray)
	{
		BufferedImage img = null;
		try
		{
			img = read(byteArray);
		}
		catch (IOException e)
		{
			log.log(Level.SEVERE, "Reading image failed.", e);
		}
		return img;
	}
1054068
public void draw(Canvas canvas) {
    for (int i = 0; i < mHolders.size(); ++i) {
      Drawable drawable = get(i).getTopLevelDrawable();
      if (drawable != null) {
        drawable.draw(canvas);
      }
    }
  }
684754
static public byte[] getBytes(StringBuffer data) {
        if (null == data) {
            return null;
        }
        int len = data.length();
        char[] chars = new char[len];
        data.getChars(0, len, chars, 0);
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++) {
            bytes[i] = (byte) chars[i];
        }
        return bytes;
    }
755981
public int[][] readImage() throws IOException {
        int[][] dat = new int[getRows()][getCols()];
        for (int i = 0; i < getCols(); i++) {
            for (int j = 0; j < getRows(); j++) {
                dat[i][j] = readUnsignedByte();
            }
        }
        return dat;
    }
699993
@Nullable
    public static Bitmap readImageFromPath(String imagePath, int width, int height) {
        File imageFile = new File(imagePath);
        if (imageFile.exists()) {
            try {
                BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(imageFile));
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(inputStream, null, options);
                inputStream.close();

                options.inJustDecodeBounds = false;
                options.inSampleSize = computeSampleSize(options, width, height);

                Bitmap sampledBitmap = null;
                boolean attemptSuccess = false;
                while (!attemptSuccess) {
                    inputStream = new BufferedInputStream(new FileInputStream(imageFile));
                    try {
                        sampledBitmap = BitmapFactory.decodeStream(inputStream, null, options);
                        attemptSuccess = true;
                    } catch (Exception e) {
                        options.inSampleSize *= 2;
                    }
                    inputStream.close();
                }

                String lowerPath = imagePath.toLowerCase();
                if (lowerPath.endsWith(".jpg") || lowerPath.endsWith(".jpeg")) {
                    int degrees = computeDegree(imagePath);
                    if (degrees > 0) {
                        Matrix matrix = new Matrix();
                        matrix.setRotate(degrees);
                        Bitmap newBitmap = Bitmap.createBitmap(sampledBitmap, 0, 0, sampledBitmap.getWidth(), sampledBitmap.getHeight(), matrix, true);
                        if (newBitmap != sampledBitmap) {
                            sampledBitmap.recycle();
                            sampledBitmap = newBitmap;
                        }
                    }
                }
                return sampledBitmap;
            } catch (Exception ignored) {
            }
        }
        return null;
    }
43873
public BufferedImage getImage() throws IOException {
        if (image == null) {
            // No content, no image
            if (bufferedOut == null) {
                return null;
            }

            // Read from the byte buffer
            InputStream byteStream = bufferedOut.createInputStream();
            ImageInputStream input = null;
            try {
                input = ImageIO.createImageInputStream(byteStream);
                Iterator readers = ImageIO.getImageReaders(input);
                if (readers.hasNext()) {
                    // Get the correct reader
                    ImageReader reader = (ImageReader) readers.next();
                    try {
                        reader.setInput(input);

                        ImageReadParam param = reader.getDefaultReadParam();

                        // Get default size
                        int originalWidth = reader.getWidth(0);
                        int originalHeight = reader.getHeight(0);
//////////////////
// PRE-PROCESS (prepare): param, size, format?, request, response?
                        // TODO: AOI strategy?
                        // Extract AOI from request
                        Rectangle aoi = extractAOIFromRequest(originalWidth, originalHeight, originalRequest);

                        if (aoi != null) {
                            param.setSourceRegion(aoi);
                            originalWidth = aoi.width;
                            originalHeight = aoi.height;
                        }

                        // TODO: Size and subsampling strategy?
                        // If possible, extract size from request
                        Dimension size = extractSizeFromRequest(originalWidth, originalHeight, originalRequest);
                        double readSubSamplingFactor = getReadSubsampleFactorFromRequest(originalRequest);

                        if (size != null) {
                            //System.out.println("Size: " + size);
                            if (param.canSetSourceRenderSize()) {
                                param.setSourceRenderSize(size);
                            }
                            else {
                                int subX = (int) Math.max(originalWidth / (size.width * readSubSamplingFactor), 1.0);
                                int subY = (int) Math.max(originalHeight / (size.height * readSubSamplingFactor), 1.0);

                                if (subX > 1 || subY > 1) {
                                    param.setSourceSubsampling(subX, subY, subX > 1 ? subX / 2 : 0, subY > 1 ? subY / 2 : 0);
                                }
                            }
                        }

                        // Need base URI for SVG with links/stylesheets etc
                        maybeSetBaseURIFromRequest(param);

/////////////////////

                        // Finally, read the image using the supplied parameter
                        BufferedImage image = reader.read(0, param);

                        // TODO: If we sub-sampled, it would be a good idea to blur before resampling,
                        // to avoid jagged lines artifacts

                        // If reader doesn't support dynamic sizing, scale now
                        image = resampleImage(image, size);

                        // Fill bgcolor behind image, if transparent
                        extractAndSetBackgroundColor(image); // TODO: Move to flush/POST-PROCESS

                        // Set image
                        this.image = image;
                    }
                    finally {
                        reader.dispose();
                    }
                }
                else {
                    context.log("ERROR: No suitable image reader found (content-type: " + originalContentType + ").");
                    context.log("ERROR: Available formats: " + getFormatsString());

                    throw new IIOException("Unable to transcode image: No suitable image reader found (content-type: " + originalContentType + ").");
                }

                // Free resources, as the image is now either read, or unreadable
                bufferedOut = null;
            }
            finally {
                if (input != null) {
                    input.close();
                }
            }
        }

        // Image is usually a BufferedImage, but may also be a RenderedImage
        return image != null ? ImageUtil.toBuffered(image) : null;
    }
28813
private void readBitmap() {
        // (sub)image position & size.
        header.currentFrame.ix = readShort();
        header.currentFrame.iy = readShort();
        header.currentFrame.iw = readShort();
        header.currentFrame.ih = readShort();

        int packed = read();
        // 1 - local color table flag interlace
        boolean lctFlag = (packed & 0x80) != 0;
        int lctSize = (int) Math.pow(2, (packed & 0x07) + 1);
        // 3 - sort flag
        // 4-5 - reserved lctSize = 2 << (packed & 7); // 6-8 - local color
        // table size
        header.currentFrame.interlace = (packed & 0x40) != 0;
        if (lctFlag) {
            // Read table.
            header.currentFrame.lct = readColorTable(lctSize);
        } else {
            // No local color table.
            header.currentFrame.lct = null;
        }

        // Save this as the decoding position pointer.
        header.currentFrame.bufferFrameStart = rawData.position();

        // False decode pixel data to advance buffer.
        skipImageData();

        if (err()) {
            return;
        }

        header.frameCount++;
        // Add image to frame.
        header.frames.add(header.currentFrame);
    }
1337010
public static byte[] bufferedImageToBMPByteArray(BufferedImage image, BufferedImage bufferedImage) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            /* ImageTypeSpecifier type =
             ImageTypeSpecifier.createFromRenderedImage(image);
             Iterator<ImageWriter> iter = ImageIO.getImageWriters(type, "bmp");
             System.err.println(ImageIO.getWriterFormatNames());
             boolean sig=iter.hasNext();*/
            //BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D ngraphics = (Graphics2D) bufferedImage.getGraphics();
            /*ngraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
             RenderingHints.VALUE_INTERPOLATION_BILINEAR);*/
            //ngraphics.drawImage(image, 0, 0,512,384,0,0,image.getWidth(),image.getHeight(), null);        	
            //ngraphics.drawImage(image.getScaledInstance(200, 300, BufferedImage.SCALE_FAST), 0, 0,null);        	
            ngraphics.drawImage(image, 0, 0, null);
            ImageIO.write(bufferedImage, "bmp", baos);
            baos.flush();
            byte[] imageInByte = baos.toByteArray();
            baos.close();
            return imageInByte;
        } catch (IOException ex) {
            Logger.getLogger(PHATImageUtils.class.getName()).log(Level.SEVERE, "bufferedImageToByteArray", ex);
        }
        return null;
    }
374866
function getImageData (canvasId, x, y, width, height, done) {
  wx.canvasGetImageData({
    canvasId,
    x,
    y,
    width,
    height,
    success (res) {
      done(res, null)
    },
    fail (res) {
      done(null, res)
    }
  })
}
1093346
private ImageMetaData readImageMetaData() {
    InputStream inputStream = null;
    ImageMetaData metaData = null;
    try {
      inputStream = getInputStream();
      metaData = BitmapUtil.decodeDimensionsAndColorSpace(inputStream);
      mColorSpace = metaData.getColorSpace();
      Pair<Integer, Integer> dimensions = metaData.getDimensions();
      if (dimensions != null) {
        mWidth = dimensions.first;
        mHeight = dimensions.second;
      }
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          // Head in the sand
        }
      }
    }
    return metaData;
  }