private Image scaleImage(URL imageURL, int width, int height) { try { Image scaledBimg = null; ContentRepositoryRegistry registry = ContentRepositoryRegistry.getInstance(); ContentRepository cr = registry.getRepository(LoginManager.getPrimary()); String[] urls = imageURL.toString().split("/"); String fname = urls[urls.length - 1]; String userName = urls[3]; ContentCollection user = cr.getUserRoot(userName); ContentResource res = (ContentResource) user.getChild(fname); BufferedImage bimg = ImageIO.read(res.getInputStream()); if (bimg.getWidth() > width || bimg.getHeight() > height) { if (bimg.getWidth() > width && bimg.getHeight() > height) { if (bimg.getWidth() - width > bimg.getHeight() - height) { float nw = width; float nh = (width * bimg.getHeight()) / bimg.getWidth(); scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH); // scaledBimg.getGraphics().drawImage(bimg, 0, 0, null); } else { float nh = height; float nw = (height * bimg.getWidth()) / bimg.getHeight(); scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH); // scaledBimg.getGraphics().drawImage(bimg, 0, 0, null); } } else if (bimg.getWidth() > width) { float nw = width; float nh = (width * bimg.getHeight()) / bimg.getWidth(); scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH); } else if (bimg.getHeight() > height) { float nh = height; float nw = (height * bimg.getWidth()) / bimg.getHeight(); scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH); } } else { scaledBimg = bimg; } return scaledBimg; } catch (Exception ex) { ex.printStackTrace(); Logger.getLogger(CoverScreen.class.getName()).log(Level.SEVERE, null, ex); } return null; }
public File uploadFileAudioSource(String audioSource) throws AudioCacheHandlerException { // make sure specified file exists, create an // entry in the content repository and upload the file. String pattern = "file://"; String s = audioSource; int ix = audioSource.indexOf(pattern); if (ix >= 0) { s = s.substring(ix + pattern.length()); } File file = new File(s); if (file.exists() == false) { throw new AudioCacheHandlerException("Nonexistent file to upload " + s); } logger.warning("Upload File: " + s); ContentRepositoryRegistry registry = ContentRepositoryRegistry.getInstance(); ContentRepository repo = registry.getRepository(LoginManager.getPrimary()); ContentCollection audioCollection = null; try { ContentCollection c = repo.getUserRoot(); audioCollection = (ContentCollection) c.getChild("audio"); if (audioCollection == null) { audioCollection = (ContentCollection) c.createChild("audio", Type.COLLECTION); } } catch (ContentRepositoryException e) { throw new AudioCacheHandlerException("Content repository exception: " + e.getMessage()); } try { /* * Remove file if it exists. */ ContentResource r = (ContentResource) audioCollection.removeChild(file.getName()); } catch (Exception e) { } try { ContentResource r = (ContentResource) audioCollection.createChild(file.getName(), ContentNode.Type.RESOURCE); r.put(file); } catch (Exception e) { throw new AudioCacheHandlerException("Failed to upload file: " + e.getMessage()); } return file; }