Example usage for java.awt.image BufferedImage getWidth

List of usage examples for java.awt.image BufferedImage getWidth

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getWidth.

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:TextureByReference.java

public static BufferedImage convertImage(BufferedImage bImage, int type) {
    int width = bImage.getWidth();
    int height = bImage.getHeight();
    BufferedImage newImage = new BufferedImage(width, height, type);
    int[] rgbArray = new int[width * height];
    bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
    newImage.setRGB(0, 0, width, height, rgbArray, 0, width);
    return newImage;
}

From source file:MainClass.java

public BufferedImage emboss(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dst;/* ww w  . j  av  a2 s  .co  m*/
    dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            int upperLeft = 0;
            int lowerRight = 0;

            if (i > 0 && j > 0)
                upperLeft = src.getRGB(j - 1, i - 1);

            if (i < height - 1 && j < width - 1)
                lowerRight = src.getRGB(j + 1, i + 1);

            int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255);

            int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255);

            int blueDiff = (lowerRight & 255) - (upperLeft & 255);

            int diff = redDiff;
            if (Math.abs(greenDiff) > Math.abs(diff))
                diff = greenDiff;
            if (Math.abs(blueDiff) > Math.abs(diff))
                diff = blueDiff;

            int grayColor = 128 + diff;

            if (grayColor > 255)
                grayColor = 255;
            else if (grayColor < 0)
                grayColor = 0;

            int newColor = (grayColor << 16) + (grayColor << 8) + grayColor;

            dst.setRGB(j, i, newColor);
        }

    return dst;
}

From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java

/**
 * ???????/*from   w  ww. ja va 2 s . c  o m*/
 * 
 * @param org_id
 * @param folderName
 * @param uid
 * @param fileBean
 * @param acceptExts
 * @param msgList
 * @return
 */
public static ShrinkImageSet getBytesShrinkFilebean(String org_id, String folderName, int uid,
        FileuploadLiteBean fileBean, String[] acceptExts, int width, int height, List<String> msgList,
        boolean isFixOrgImage) {

    byte[] result = null;
    byte[] fixResult = null;
    InputStream is = null;
    boolean fixed = false;

    try {

        String file_name = fileBean.getFileName();
        String ext = "";

        if (acceptExts != null && acceptExts.length > 0) {
            // ???
            // ????
            boolean isAccept = false;
            String tmpExt = null;
            int len = acceptExts.length;
            for (int i = 0; i < len; i++) {
                if (!acceptExts[i].startsWith(".")) {
                    tmpExt = "." + acceptExts[i];
                }
                if (file_name.toLowerCase().endsWith(tmpExt)) {
                    isAccept = true;
                    ext = tmpExt.replace(".", "");
                    ;
                    break;
                }
            }
            if (!isAccept) {
                // ???????null ?
                return null;
            }
        }

        is = ALStorageService.getFile(FOLDER_TMP_FOR_ATTACHMENT_FILES,
                uid + ALStorageService.separator() + folderName, String.valueOf(fileBean.getFileId()));

        byte[] imageInBytes = IOUtils.toByteArray(is);
        ImageInformation readImageInformation = readImageInformation(new ByteArrayInputStream(imageInBytes));
        BufferedImage bufferdImage = ImageIO.read(new ByteArrayInputStream(imageInBytes));
        if (readImageInformation != null) {
            bufferdImage = transformImage(bufferdImage, getExifTransformation(readImageInformation),
                    readImageInformation.orientation >= 5 ? bufferdImage.getHeight() : bufferdImage.getWidth(),
                    readImageInformation.orientation >= 5 ? bufferdImage.getWidth() : bufferdImage.getHeight());
            fixed = isFixOrgImage;
        }
        if (bufferdImage == null) {
            // ?bufferdImage???????????,null?.
            return null;
        }

        BufferedImage shrinkImage = FileuploadUtils.shrinkAndTrimImage(bufferdImage, width, height);
        Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpeg");
        ImageWriter writer = writers.next();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(out);
        writer.setOutput(ios);
        writer.write(shrinkImage);

        result = out.toByteArray();

        if (fixed) {
            Iterator<ImageWriter> writers2 = ImageIO.getImageWritersBySuffix(ext);
            ImageWriter writer2 = writers2.next();

            ByteArrayOutputStream out2 = new ByteArrayOutputStream();
            ImageOutputStream ios2 = ImageIO.createImageOutputStream(out2);
            writer2.setOutput(ios2);
            writer2.write(bufferdImage);

            fixResult = out2.toByteArray();
        }

    } catch (Exception e) {
        logger.error("fileupload", e);
        result = null;
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Exception e) {
            logger.error("fileupload", e);
            result = null;
        }
    }

    return new ShrinkImageSet(result, fixed ? fixResult : null);
}

From source file:main.java.whiteSocket.Bloop.java

public static BufferedImage createWhiteImage() {
    BufferedImage testOut = new BufferedImage(Bloop.blooprint.getWidth(), Bloop.blooprint.getHeight(),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = testOut.createGraphics();
    graphics.setPaint(Color.white);
    graphics.fillRect(0, 0, testOut.getWidth(), testOut.getHeight());
    return testOut;
}

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static int[][][] getRGBArray(BufferedImage imageIn) {
    // possible 10-fold speed increase in:
    // http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image
    // (but ensure all major bases are covered)
    int ImW = imageIn.getWidth();
    int ImH = imageIn.getHeight();
    Color tmpColor;//  w  ww . ja v  a2s  . c  o  m
    int[][][] rgbValues = new int[3][ImW][ImH];

    for (int ii = 0; ii < ImW; ii++) {
        for (int jj = 0; jj < ImH; jj++) {
            tmpColor = new Color(imageIn.getRGB(ii, jj));
            rgbValues[0][ii][jj] = tmpColor.getRed();
            rgbValues[1][ii][jj] = tmpColor.getGreen();
            rgbValues[2][ii][jj] = tmpColor.getBlue();
        }
    }
    return rgbValues;
}

From source file:Main.java

ScreenCaptureRectangle(final BufferedImage screen) {
    BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
    JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
    JScrollPane screenScroll = new JScrollPane(screenLabel);

    screenScroll.setPreferredSize(new Dimension(300, 300));

    repaint(screen, screenCopy);/*from   w  ww.  ja v  a 2  s  .c  om*/
    screenLabel.repaint();

    screenLabel.addMouseMotionListener(new MouseMotionAdapter() {
        Point start = new Point();

        @Override
        public void mouseMoved(MouseEvent me) {
            start = me.getPoint();
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            Point end = me.getPoint();
            captureRect = new Rectangle(start, new Dimension(end.x - start.x, end.y - start.y));
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }
    });
    JOptionPane.showMessageDialog(null, screenScroll);
}

From source file:ImageOpByRomain.java

/**
 * <p>/*w  ww.jav a 2 s  . c  om*/
 * Returns a new <code>BufferedImage</code> using the same color model as
 * the image passed as a parameter. The returned image is only compatible with
 * the image passed as a parameter. This does not mean the returned image is
 * compatible with the hardware.
 * </p>
 * 
 * @param image
 *            the reference image from which the color model of the new image
 *            is obtained
 * @return a new <code>BufferedImage</code>, compatible with the color
 *         model of <code>image</code>
 */
public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {
    ColorModel cm = image.getColorModel();
    return new BufferedImage(cm, cm.createCompatibleWritableRaster(image.getWidth(), image.getHeight()),
            cm.isAlphaPremultiplied(), null);
}

From source file:app.model.game.CoverUploadModel.java

public void uploadImage(HttpServletRequest req, Game g)
        throws FileUploadBase.SizeLimitExceededException, FileUploadException, SQLException, Exception {
    int width = 500;
    int height = 800;

    //Bild upload
    FileUpload upload = new FileUpload(2000 * 1024, 5000 * 1024,
            "C:/Users/Public/Arcade/Games/" + g.getGameID() + "/assets",
            "C:/Users/Public/Arcade/Games/" + g.getGameID() + "/tmp/");
    File fileStatus;//  ww w  .  j a v  a 2  s.  c  o  m

    fileStatus = upload.uploadFile(req);
    File file = new File(fileStatus.getAbsolutePath());

    BufferedImage img = ImageIO.read(file);
    if (img.getWidth() != width || img.getHeight() != height) {
        img = Scalr.resize(img, Scalr.Method.SPEED, Scalr.Mode.FIT_EXACT, 500, 800);
    }
    File destFile = new File(fileStatus.getParent() + "/" + g.getGameID() + ".jpg");
    ImageIO.write(img, "jpg", destFile);
    g.updateState("coverupload", "complete");
    String state = g.stateToJSON();

    try (SQLHelper sql = new SQLHelper()) {
        sql.execNonQuery("UPDATE `games` SET editState='" + state + "' WHERE ID = " + g.getGameID());
    }

    fileStatus.delete();
}

From source file:eu.novait.imageresizer.helpers.ImageConverter.java

public void process() throws IOException {
    BufferedImage inImage = ImageIO.read(this.file);
    double ratio = (double) inImage.getWidth() / (double) inImage.getHeight();
    int w = this.width;
    int h = this.height;
    if (inImage.getWidth() >= inImage.getHeight()) {
        w = this.width;
        h = (int) Math.round(w / ratio);
    } else {/*w w w.ja v  a  2 s  . c  o m*/
        h = this.height;
        w = (int) Math.round(ratio * h);
    }
    int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType();
    BufferedImage outImage = new BufferedImage(w, h, type);
    Graphics2D g = outImage.createGraphics();
    g.drawImage(inImage, 0, 0, w, h, null);
    g.dispose();
    String ext = FilenameUtils.getExtension(this.file.getAbsolutePath());
    String t = "jpg";
    switch (ext) {
    case "png":
        t = "png";
        break;
    }
    ImageIO.write(outImage, t, this.outputfile);
}

From source file:GraphicsUtil.java

public void drawCentered(BufferedImage img, Point2D location) {
    drawCentered(g, img, location, img.getWidth(), img.getHeight(), observer);
}