I am trying to export TeeChart as Image on Xamarin Android without showing the chart. It creates the file on external storage. However, the file is broken and cannot be loaded.
Do you know if it is possible? If it possible, can you give me a sample code doing that?
You can use this to get the view's bitmap and storage it:
Steema.TeeChart.TChart tChart1= new Steema.TeeChart.TChart(this);
Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar();
tChart1.Series.Add(bar1);
bar1.Add(3, "Pears", System.Drawing.Color.Red);
bar1.Add(4, "Apples", System.Drawing.Color.Blue);
bar1.Add(2, "Oranges", System.Drawing.Color.Green);
Steema.TeeChart.Themes.BlackIsBackTheme theme = new Steema.TeeChart.Themes.BlackIsBackTheme(tChart1.Chart);
theme.Apply();
// here you can get the view' bitmap
tChart1.DrawingCacheEnabled = true;
tChart1.BuildDrawingCache();
Bitmap viewBitmap = tChart1.DrawingCache;
//storage the bitmap.
FileStream stream = File.OpenWrite(FilesDir.AbsolutePath + "111111.jpg");
viewBitmap.Compress(Bitmap.CompressFormat.Jpeg, 90, stream);
Here is the setDrawingCacheEnabled method, which allow you get view's bitmap.
Related
A moment when I click a notification bar, I want app to capture current screen. But i want the app capture 'after the notification bar disappears'. And I want that captured screen to be saved and loaded right after and showed up. How shoud i do?
For example code just do template of "new full screen activity" this will show you how to show/hide toolbar and make full screen.
Then just modify the code so it doesn't happen on a timer, instead move it to a touch listener that you place on the toolbar itself.
Lastly, once the fade has disappeared (which you can do instant if you want) then simply make your own screenshot. There are plenty of great libraries that will do this for you if you want or you can use simple code that I put in a class I named ImageHelper like this:
public static Bitmap takeScreenshotOfView(Activity context, Bitmap.CompressFormat compressFormat){
Bitmap screenshot = null;
try {
// create bitmap screen capture
View v1 = context.getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(context.getFilesDir() + File.separator + "A35_temp" + File.separator + "screenshot_temp");
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
screenshot.compress(compressFormat, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
return screenshot;
}
And just use it like:
Bitmap screenshot = ImageHelper.takeScreenshotOfView(this, Bitmap.CompressFormat.JPEG);
Next start a fullScreen Activity new for Preview but default with toolbar hidden and fullscreen ImageView with your share overlay buttons or whatever you want at the bottom.
I tried to use FreeImage library to load PNG as a texture (from memory). That's the fragment of code:
FIMEMORY *fiStream = FreeImage_OpenMemory(streamData, size);
FREE_IMAGE_FORMAT fileFormat = FreeImage_GetFileTypeFromMemory(fiStream, 0);
FIBITMAP *image = FreeImage_LoadFromMemory(fileFormat, fiStream, 0);
int bitsPerPixel = FreeImage_GetBPP(image);
width = (int)FreeImage_GetWidth(image);
height = (int)FreeImage_GetHeight(image);
I'm using FILE with fopen to open file and then read stream to streamData object. File and stream is read correctly.
The result is: fileFormat = -1 and image is NULL.
I also tried to use FreeImage to load PNG file directly from disk using FreeImage_Load, but the result is the same - it returns NULL.
Has anybody faced similar problem? Can you suggest an alternative to FreeImage that can read data from memory?
try this code to load your image from memory:
File file = new File("/sdcard/Images/image1.jpg");
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageBitmap(bitmap);
}
i have a pdf file(attached).
My objective is to convert a pdf to an image using pdfbox AS IT IS,(same as using snipping tool in windows).
The pdf has all kinds of shapes and text .
i am using the following code:
PDDocument doc = PDDocument.load("Hello World.pdf");
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(67);
BufferedImage bufferedImage = firstPage.convertToImage(imageType,screenResolution);
ImageIO.write(bufferedImage, "png",new File("out.png"));
when i use the code, the image file gives totally wrong outputs(out.png attached)
how do i make pdfbox take something like a direct snapshot image?
also, i noticed that the image quality of the png is not so good, is there any way to increase the resolution of the generated image?
EDIT:
here is the pdf(see page number 68)
https://drive.google.com/file/d/0B0ZiP71EQHz2NVZUcElvbFNreEU/edit?usp=sharing
EDIT 2:
it seems that all the text isvanishing.
i also tried using the PDFImageWriter class
test.writeImage(doc, "png", null, 68, 69, "final.png",TYPE_USHORT_GRAY,200 );
same result
Using PDFRenderer it is possible to convert PDF page into image formats.
Convert PDF page into image in java Using PDF Renderer. Jars Required PDFRenderer-0.9.0
package com.pdfrenderer.examples;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PdfToImage {
public static void main(String[] args) {
try {
String sourceDir = "C:/Documents/Chemistry.pdf";// PDF file must be placed in DataGet folder
String destinationDir = "C:/Documents/Converted/";//Converted PDF page saved in this folder
File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
String fileName = sourceFile.getName().replace(".pdf", "_cover");
if (sourceFile.exists()) {
if (!destinationFile.exists()) {
destinationFile.mkdir();
System.out.println("Folder created in: "+ destinationFile.getCanonicalPath());
}
RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
int pageNumber = 62;// which PDF page to be convert
PDFPage page = pdf.getPage(pageNumber);
System.out.println("Total pages:"+ pdf.getNumPages());
// create the image
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
// width & height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done
Image image = page.getImage(rect.width, rect.height, rect, null, true, true );
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, null);
File imageFile = new File( destinationDir + fileName +"_"+ pageNumber +".png" );// change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp
ImageIO.write(bufferedImage, "png", imageFile);
System.out.println(imageFile.getName() +" File created in: "+ destinationFile.getCanonicalPath());
} else {
System.err.println(sourceFile.getName() +" File not exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ConvertedImage:
I get the same result as the OP using PDFBox version 1.8.4. In version 2.0.0-SNAPSHOT, though, it looks better:
Here only some arrows are thinner and some arrow parts are mis-drawn as boxes.
Thus,
how do i make pdfbox take something like a direct snapshot image?
The current release versions (up to 1.8.4) seem to have greater deficits when rendering PDFs as images. You may switch to a current development version (e.g. the current trunk, 2.0.0-SNAPSHOT) or wait until the improvements are released.
Furthermore, some minor deficits are even in 2.0.0-SNAPSHOT. You might want to present your sample document to the PDFBox people (i.e. create an according issue in their JIRA) so that they improve PDFBox even further to suit your needs.
also, i noticed that the image quality of the png is not so good, is there any way to increase the resolution of the generated image?
There are convertToImage overloads with resolution parameters. Your current code actually sets the resolution to screenResolution. Increase this resolution value.
PS: The code to render a PDF page to image has been refactored in 2.0.0-SNAPSHOT. Instead of
BufferedImage image = page.convertToImage();
you now do
BufferedImage image = RenderUtil.convertToImage(page);
I assume this has been done to remove direct AWT references from the core classes because AWT is not available on e.g. Android.
PS: The SNAPSHOT I used last year in this answer merely was a snapshot subject to changes. The 2.0.0 release is still under development, many things have changed. Especially there is no RenderUtil class anymore. Instead one currently has to use the PDFRenderer in the org.apache.pdfbox.rendering package...
it turns out that jpedal(lgpl) does the converting perfectly(just like a snapshot).
here is what I have used :
PdfDecoder decode_pdf = new PdfDecoder(true);
FontMappings.setFontReplacements();
decode_pdf.openPdfFile("Hello World.pdf");
decode_pdf.setExtractionMode(0,800,3);
try {
for(int i=0;i<40;i++)
{
BufferedImage img=decode_pdf.getPageAsImage(2+i);
ImageIO.write(img, "png",new File(String.valueOf(i)+"out.png"));
}
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
decode_pdf.closePdfFile();
} catch (PdfException e) {
e.printStackTrace();
}
it works fine.
My app takes images from gallery and copies to a subfolder. But, the images that are copied are comes to gallery as a copy of original one from different location.
How to prevent it? please help me.
this is my code to save to a pre defined folder....
protected String saveBitmap(Bitmap bm, String path) throws Exception {
String tempFilePath="/sdcard/AuFridis/Events/Images/"+System.currentTimeMillis()+"myEventImg.jpg";
File tempFile = new File(path+"/"+System.currentTimeMillis()+"myEventImg.jpg");
// File tempFile = new File("/sdcard/Notes");
tempFile.createNewFile();
if (!tempFile.exists()) {
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
}
//tempFile.delete();
//tempFile.createNewFile();
int quality = 100;
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bm.compress(CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
//bm.recycle();
Log.i("On saveBitmap Function - retrieved file path", "---"+tempFilePath);
return tempFilePath;
}
Are you trying to hide the images from the gallery? Android by default scans the memory of the device and puts all photos it finds into the gallery. The device is most likely seeing the repeated name and labeling. Just change the image name.
By default, android scans the SD card and adds all images it finds to the gallery. If you copy an image from gallery to another folder, that folder will automatically be added to the gallery. To prevent this, add an empty file with name .nomedia to your destination folder - and your copied images will not show in the gallery.
I am creating one wallpaper application therefore i put some image in asset folder. I need to show this image one by one on button click and store it in sd card.
What i did:
I use ImageView and WebView to show image. First, when i use WebView, i stuck on setting image size because it showing to small and i need to show those image as per device window size.
I use following code but didn't help to adjust image on screen
myWebView.loadUrl("file:///android_asset/image.html");
WebSettings settings = myWebView.getSettings();
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
I also set <src img="someimage.jpg" width=""100%"> but it didn't help me.
Then i use ImageView to show image and able to show image at least in some proper size using following code.
InputStream ims = getAssets().open("31072011234.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imageView.setImageDrawable(d);
My Question is
Which is the good way to show image on screen imageview or webview?. how to take all picture in array when i don't know name of this images and store it in SD card
Give me some hint or reference.
Thanks in advance.
You don't need to put your images in assets folder, you can use res/drawable to store your images and access it as resource.
Using below code you can access images from drawable without need to know the names of image files.
Class resources = R.drawable.class;
Field[] fields = resources.getFields();
String[] imageName = new String[fields.length];
int index = 0;
for( Field field : fields )
{
imageName[index] = field.getName();
index++;
}
int result = getResources().getIdentifier(imageName[10], "drawable", "com.example.name");
and using below code you can save your images to SD card.
File file = new File(extStorageDirectory, "filename.PNG");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
The best way to show an image would be ImageView (That's why it's called an Image View), I recommend that you add the image in res/drawable folder and show the image using:
imageView.setImageResource(R.id.some_image);
The resource can be saved to sdcard using:
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.id.some_image);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "someimage.PNG");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();