Primefaces Print Dynamic Images - image

I tried to load an image dynamically, and everything works.
The image I is loaded and displayed correctly in a dynamic,
I added the tag for printing.
Now if I ask to print the image created dynamically I can not print.
<pou:commandButton value="Print" type="button" icon="ui-icon-print">
<pou:printer target="image" />
</pou:commandButton>
<pou:graphicImage id="image" value="#{printDynamicBean.graphicIMG}" />
My bean does like this:
public StreamedContent getGraphicIMG() {
//Graphic
BufferedImage bufferedImg;
try {
bufferedImg = ImageIO.read(baseImage);
} catch (IOException e) {
}
try {
Graphics2D g2 = bufferedImg.createGraphics();
g2.setColor(Color.black);
int style = Font.BOLD | Font.ITALIC;
Font f1 = new Font("TimesNewRoman", style , 60);
g2.setFont(f1);
g2.drawString("Hello Print", 80, 580);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImg, "png", os);
graphicIMG = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
} catch (IOException ex) {
Logger.getLogger(PrintCartelliniBean.class.getName()).log(Level.SEVERE, null, ex);
}
return graphicIMG;
}
it is as if she had forgotten the image created.
Thanks.

using CDI bean you can do this :
#Model
public class ImageBean {
private StreamedContent image;
#Produces
#Named
public StreamedContent getImage() {
if (FacesContext.getCurrentInstance().getRenderResponse()) {
// rendering the view, return a stub StreamedContent to generate right URL.
image = new DefaultStreamedContent();
} else {
// requesting the image
image = your dynamic image;
}
return image;
}
}
in your view : <pou:graphicImage id="image" value="#{image}" />

Related

Not able to convert an image URL to Bitmap, in Xamarin.android

I need to convert a string image https URL to Bitmap, the solution in stack overflow does not work for me.
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}`
If i use this code, it gives me many reference error in C#.
According to your description, I guess that you want to download image from url and convert into bitmap?
If yes, I suggest you can take a look the following code:
private Bitmap GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
Then you can display this image in ImageView.
private ImageView image;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
image = FindViewById<ImageView>(Resource.Id.imageView1);
var imageBitmap = GetImageBitmapFromUrl("https://cdn1.iconfinder.com/data/icons/general-9/500/add-512.png");
image.SetImageBitmap(imageBitmap);
}
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView1"/>

Show an image from local computer in Image with model binding

I want to implement an feature like that, an image control in xamarin.uwp, a user will select the image by file picker, and set the the image to the image control.
As far as I know, I could not set the local path directly to the file path.
How can I use with the file stream?
Update1:
xaml
<Image Grid.Row="3" Source="{Binding Image}"></Image>
ImagePageViewModel
public class ImagePageViewModel : BindableBase
{
public ImagePageViewModel()
{
}
private ImageSource _image;
public ImageSource Image
{
get { return _image; }
set { SetProperty(ref _image, value); }
}
private DelegateCommand _imageSelect;
public DelegateCommand ImageSelect =>
_imageSelect ?? (_imageSelect = new DelegateCommand(ExecuteImageSelect));
async void ExecuteImageSelect()
{
try
{
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
};
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
Image = ImageSource.FromStream(() => fileStream.AsStream());
}
}
else
{
ImageUri = "";
}
}
catch (Exception ex)
{
throw ex;
}
}
}

Xamarin.Forms Xamarin Android

SaveFileDialog in XamarinForms
My requirement is to save a file in Xamarin.Forms like below image and what ever I tried so far is:
public class CustomWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged (e);
if (e.NewElement != null)
{
var customWebView = Element as CustomWebView;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
string root = Android.OS.Environment.ExternalStorageDirectory.ToString();
// Java.IO.File myDir = new Java.IO.File(root + "/WingsPdfs");
// myDir.Mkdir();
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Java.IO.File file = new Java.IO.File(path, "WingsPdfGenerated.pdf");
FileOutputStream outs = new FileOutputStream(file);
outs.Write(customWebView.PdfStream.ToArray());
outs.Flush();
outs.Close();
try
{
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", file));
}
catch(Exception ex)
{
}
}
}
}
In the above code I hardcoded the location of the file but I want user to select particular location and save to the selected location.
Thank You
enter image description here

Can´t render upload images in Magnolia CMS

i have a content app in Magnolia CMS, now i need to redner a image of the class DamUploadFieldDefinition from a content app
jcr node
I have tried this in my .ftl:
<img src = "${cmsfn.link(news.jcrPhoto)}" />
But only return a path of the photo and can´t render my photo
Searching i found this solution but doesn´t render:
<img src="${damfn.getRendition(news.jcrPhoto, "myAssetVariante").link}" />
I was looking in another content app named "contacts" of MAgnolia, and they make it through a model class:
public class NewsModel<RD extends TemplateDefinition> extends AbstractSTKTemplateModel<TemplateDefinition> {
private Node news;
#Inject
public NewsModel(Node content, TemplateDefinition definition, RenderingModel<?> parent,
STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
super(content, definition, parent, stkFunctions, templatingFunctions);
System.out.println("Entramos en el constructor");
}
/**
* FIXME: should be done better (binaryHandling): SCRUM-525.
*/
public String getPhoto() {
System.out.println("BOB inicio en el getPhoto");
if (news == null) {
System.out.println("news == null");
return null;
}
Property binaryData = null;
try {
if (news.hasNode("photo")) {
System.out.println("Tenemos contenido");
Node binaryNode = news.getNode("photo");
binaryData = binaryNode.getProperty("jcr:data");
}
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
if (binaryData != null) {
System.out.println("retornamos desde templatingFunctions");
return templatingFunctions.link(binaryData);
} else {
System.out.println("retornamos null");
return null;
}
}
public ContentMap getNews() {
System.out.println("Inicio getNews");
ContentMap cm = templatingFunctions.asContentMap(news);
System.out.println("ContentMap=\n"+cm);
return cm;
}
public void setNews(Node news) {
this.news = news;
}
#Override
public String execute() {
System.out.println("En el execute");
try {
NodeIterator ni = content.getNodes();
System.out.println("size:"+ni.getSize());
while(ni.hasNext()){
System.out.println(ni.toString());
Node n = ni.nextNode();
System.out.println(n.getIdentifier());
}
} catch (RepositoryException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//String id = PropertyUtil.getString(content, "news");
String id = "651c7140-b681-45b4-8814-ae26bfa0ba0d";
news = null;
System.out.println("id="+id);
if (StringUtils.isNotEmpty(id)) {
System.out.println("En el if del execute");
try {
news = new HTMLEscapingContentDecorator(true).wrapNode(NodeUtil.getNodeByIdentifier("news",id));
}
catch (RepositoryException e) {
System.out.println("Can't get uuid: '" + id + "' of contact.");
}
}
return super.execute();
}
}
And my FTL is:
[#assign news = model.news!]
7
[#if news?has_content]
8
[#assign hasPhoto = model.photo?has_content]
[/#if]
[#if contact?has_content]
9
[#if hasPhoto]
10
<dl class="media photo pos-2">
<dt><img src="${model.photo}"/></dt>
</dl>
[/#if]
[/#if]
When i put String id = PropertyUtil.getString(content, "news"); ID is null so don´t pass in if (StringUtils.isNotEmpty(id)) {
So i hardcode putting
String id = "651c7140-b681-45b4-8814-ae26bfa0ba0d";
and run all code but id doesn´t show the img
Result
Showing Google´s console i can see the image path:
/mgnl-prosegur-intra-webapp/01/photo
But even putting the src like Contacts content app with my image name, dont shows anything
<img src="/mgnl-prosegur-intra-webapp/demo-project/contacts/ldavinci/photo/vitruviano.jpeg" alt="">
Changed to should be:
<img src="/mgnl-prosegur-intra-webapp/demo-project/contacts/ldavinci/photo/vitruviano.jpeg" alt="">
But not function
Please
if image is in DAM (Assets app), try this:
<img src="${damfn.getAssetLink(news.jcrPhoto)}">

BlackBerry create bitmap from path

After taking a picture in my app I want to get the image (by it's path).
I see lots of examples on how to get an image, the only problem is that it no longer is possible doing it by using Connector.open() since Connector is deprecated now.
What to use instead?
conn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/" + picture);
try {
InputStream input = null;
input = fconn.openInputStream();
int available = 0;
available = input.available();
int fSz = (int)fconn.fileSize();
byte[] data = new byte[fSz];
input.read(data, 0, fSz);
image = EncodedImage.createEncodedImage(data,0,data.length);
bkBitmap = image.getBitmap();
} catch(ControlledAccessException e) {
pLog = "*** Problem Accessing image file:" + e;
EventLogger.logEvent( GUID, pLog.getBytes() );
}
Thanks!
Try this code:
public class LoadingScreen extends MainScreen
{
public LoadingScreen()
{
setTitle("Loading Screen");
createGUI();
}
private void createGUI()
{
BitmapField bitmapField=new BitmapField(getTheImage());
add(bitmapField);
}
private Bitmap getTheImage()
{
Bitmap bitmap=null,scaleBitmap=null;
InputStream inputStream=null;
FileConnection fileConnection=null;
try
{
fileConnection=(FileConnection) Connector.open("file:///SDCard/BlackBerry/pictures/"+"background.png");
if(fileConnection.exists())
{
inputStream=fileConnection.openInputStream();
byte[] data=new byte[(int)fileConnection.fileSize()];
data=IOUtilities.streamToBytes(inputStream);
inputStream.close();
fileConnection.close();
bitmap=Bitmap.createBitmapFromBytes(data,0,data.length,1);
//You can return this bitmap otherwise, after this you can scale it according to your requirement; like...
scaleBitmap=new Bitmap(150, 150);
bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
}
else
{
scaleBitmap=Bitmap.getBitmapResource("noimage.png");//Otherwise, Give a Dialog here;
}
}
catch (Exception e)
{
try
{
if(inputStream!=null)
{
inputStream.close();
}
if(fileConnection!=null)
{
fileConnection.close();
}
}
catch (Exception exp)
{
}
scaleBitmap=Bitmap.getBitmapResource("noimage.png");//Your known Image;
}
return scaleBitmap;//return the scale Bitmap not the original bitmap;
}
}
I got like this below Image:

Resources