How can get image from drawable folder for BitmapFactory.DecodeResource.
i tried this.
img name image kept in drawable folder then this
Bitmap originalImage = BitmapFactory.DecodeResource(Resource.Drawable.img,0);
but it's generate a error
"Can not convert from int to Android.Content.Res.Resources"
Bitmap image = BitmapFactory.DecodeResource(this.Resources,Resource.Drawable.blankImage);
The first argument returns resource for the current context, followed by your image resource link (which should be located in the 'Resource' folder)
i am able to get drawable image for bitmap decode resource.
I used this code.
string imagefileName = "imgimge.png";
// Remove the file extention from the image filename
imagefileName = imagefileName.Replace(".jpg", "").Replace(".png", "");
// Retrieving the local Resource ID from the name
int id = (int)typeof(Resource.Drawable).GetField(imagefileName).GetValue(null);
// Converting Drawable Resource to Bitmap
var originalImage = BitmapFactory.DecodeResource(Xamarin.Forms.Forms.Context.Resources, id);
Related
I'm trying to update a placeholder image with a new image that has an updated URL. The URL in fact is a valid Google Static Map URL that I'm using in other contexts successfully. I'm using the Google Document API to manipulate the document. Following the code I've been using:
var element = body.findElement(DocumentApp.ElementType.INLINE_IMAGE).getElement();
var imageMap = element.asInlineImage();
// if there was an image found in document
if (imageMap != null) {
// get current parent and index inside parent
var parent = imageMap.getParent();
var childIndex = parent.getChildIndex(imageMap);
// remove image from paragraph
imageMap = imageMap.removeFromParent();
// get static image url for territory
var url = getStaticMapURLForTerritory(id);
Logger.log(url);
imageMap.setLinkUrl(url);
// create a new image
parent.insertInlineImage(childIndex, imageMap)
}
This seems to work fine in that it does update the image url correctly. However, the image itself (the result of the url) is not updated. When I click on the link URL it does return the correct image.
Is there a way to force a refetch of the image blob associated with the URL? I've also attempted to use UrlFetchApp but that complains about a missing size parameter (google static api) which is certainly included in the url string and within the max 640x640 bounds.
I've exhausted all my options unless....
TIA, --Paul
setLinkUrl only does that: sets the link. To actually add a new image you'll have to get its blob:
function replaceImage() {
// [...]
// get static image url for territory
const url = getStaticMapURLForTerritory(id)
const response = UrlFetchApp.fetch(url)
// create a new image
parent.insertInlineImage(childIndex, response.getBlob())
.setAltDescription(img.getAltDescription())
.setAltTitle(img.getAltTitle())
.setWidth(img.getWidth())
.setHeight(img.getHeight())
.setLinkUrl(url)
}
References
Class InlineImage (Google Apps Script reference)
I am using Flutter multi_image_picker: ^4.7.14 to upload selected photos to server.
It stores images List, In my app new insert part is ok, I am getting the selected images and showing them in a gridview and sent them to the server.
But when I try to update my record, I don't understand how to get my record's images and set them to List. I have the url path of my uploaded images for that record. I want to get images and convert them to List because multi_image_picker is only accepting Asset, but there is no obvious explanation in multi_image_picker read me how to do this.
var images = List<Asset>;
for (var i = 0; i < tempList.length; i++) {
log("image link:" + ad_url + tempList[i].imageDownloadUrl);
final ByteData imageData = await NetworkAssetBundle(
Uri.parse(ad_url + tempList[i].imageDownloadUrl))
.load("");
final Uint8List bytes = imageData.buffer.asUint8List();
Image img = Image.memory(bytes);
// in this part I want to convert img to Asset , but I don't know how to do this.
Asset asset = img.... //How can I do this conversion
images.add(asset);
}
I have created a jar file for my GUI application using IntelliJ. However, when I run the file, I cannot see the toolbar which contains the buttons with ImageIcon for different functionaltites. On looking around over stackoverflow (like here: Java Swing ImageIcon, where to put images?), I found that the file path for the images could be an issue. At present I am using the following code:
OpenImagingFileButton = new JButton(createImageIcon(currentPath.concat("/src/main/java/uni/images/open-file-icon-img.png")));
The currentPath variable is obtained using this:
final String currentPath = currentRelativePath.toAbsolutePath().toString();
And the createImageIcon method has the following code:
/**
* Returns an ImageIcon, or null if the path was invalid.
*/
protected static ImageIcon createImageIcon(String path) {
ImageIcon toolBarImage = new ImageIcon(path);
Image image = toolBarImage.getImage(); // transform it
Image newImg = image.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
toolBarImage = new ImageIcon(newImg); // transform it back
return toolBarImage;
}
This code works perfectly when I run it in intelliJ, however, the toolbar is not visible when I run the jar. I also tried moving the images folder directly under src folder and then doing this:
ImageIcon(this.getClass().getResource("/images/open-file-icon-img.png"));
But this gives me aNullPointerException. Where am I going wrong?
You don’t need to use the current path here. Since you are working in IntelliJ, simply mark the folder as resources root, by selecting the src folder and right clicking it to find
mark directory as
Move the images folder under the src folder. Then use the following code to set the image icon as a button
Java - setting classpath
openProject = new JButton();
openProject.setIcon(createToolIcon("/images/openProject.png"));
And place the createToolIcon method in the same class file or create a separate class and call the method using that class.
public static ImageIcon createToolIcon(String path) {
URL url = System.class.getResource(path);
if(url == null) {
System.err.println("Unable to load Image: " + path);
}
ImageIcon icon = new ImageIcon(url);
Image img = icon.getImage();
Image newimg = img.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)
ImageIcon newIcon = new ImageIcon(newimg);
return newIcon;
}
I have an image that sits inside some folder in my MVC3 application. In my controller, how do I convert that image into a Byte array.
byte[] buffer = File.ReadAllBytes("foo.png");
and because that's inside a controller you probably want to calculate the path relative to the root:
string imageFile = Path.Combine(Server.MapPath("~/App_Data"), "foo.png");
byte[] buffer = File.ReadAllBytes(imageFile);
After using the ImagesService to transform an uploaded image, I would like to store it back into a new Blob file and make it available through getServingUrl() as provided by the ImagesService.
Storing the image in a new AppEngineFile as described here works fine and I am able to open and view it locally using the dev server.
However when passing the blobKey for the new AppEngineFile to ImagesService.getServingUrl() a
java.lang.IllegalArgumentException: Could not read blob.
exception is thrown. Any ideas what the problem could be? This is the code I use to transform and store an uploaded image (blobKey and blobInfo correspond to the uploaded file, not the newly created one).
/* Transform image in existing Blob file */
Image originalImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform verticalFlip = ImagesServiceFactory.makeVerticalFlip();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image newImage = imagesService.applyTransform(verticalFlip, originalImage);
/* Store newImage in an AppEngineFile */
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(blobInfo.getContentType());
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
ByteBuffer buffer = ByteBuffer.wrap(newImage.getImageData());
writeChannel.write(buffer);
/* closeFinally assigns BlobKey to new file object */
writeChannel.closeFinally();
BlobKey newBlobKey = fileService.getBlobKey(file);
Edit:
The above code is correct, the problem was storing a String representation of the new blob key using newBlobKey.toString() instead of newBlobKey.getKeyString().
Why would you want to do that? Once you transform an image it is cached and anyway it is always fast. If you really feel you want to save it just use urlfetch to read the data and store them in the BlobStore ;-)
The following works fine when executed at the end of the code posted in the question:
String url = imagesService.getServingUrl(newBlobKey)
The URL can then be used to scale and crop the new image as described in the docs
http://code.google.com/appengine/docs/java/images/overview.html#Transforming_Images_from_the_Blobstore