I am using itext library in which i have to put image which scale properly in landscape mode . However if i changes the page to landscape mode ,the images which holds there also gets rotated . i don't want that.And if i rotate the image it will behave differently ,it will not rotate from centre.
However ,what i want the image should not rotate ,and it should appear as follows
here is my code
Document document=new Document();
try {
File file=new File(Environment.getExternalStorageDirectory(),"mypdfimage.pdf");
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
Image image = null;
try {
image = Image.getInstance (Environment.getExternalStorageDirectory()+"/image.jpg");
int identation=0;
//Rectangle rectangle=document.getPageSize();
Rotation rotation=new Rotation();
pdfwriter.setPageEvent(rotation);
//image.scalePercent(scalerX, scalerY);
//PdfDictionary pageDict=null;
// pageDict.put(PdfName.ROTATE, new PdfNumber(90));
//pdfwriter.addPageDictEntry(PdfName.ROTATE, PdfPage.LANDSCAPE);
image.scaleToFit(PageSize.A4.getWidth() - document.leftMargin() - document.rightMargin(), PageSize.A4.getHeight() - document.topMargin() - document.bottomMargin());
// image.setRotationDegrees(90);
// image.setAlignment(Element.ALIGN_CENTER);
document.add(image);
document.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class Rotation extends PdfPageEventHelper
{
#Override
public void onStartPage(PdfWriter writer, Document document) {
writer.addPageDictEntry(PdfName.ROTATE,PdfPage.LANDSCAPE);
}
}
You are rotating the page the wrong way. You probably took an example that was written in answer to a question asking how to rotate the page and its content. That is the exception. If you follow the normal examples on how to rotate a page, the page will be rotated, but not the image.
Please take a look at the ImageOnRotatedPage example:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Image img = Image.getInstance(IMAGE);
img.scaleToFit(770, 523);
float offsetX = (770 - img.getScaledWidth()) / 2;
float offsetY = (523 - img.getScaledHeight()) / 2;
img.setAbsolutePosition(36 + offsetX, 36 + offsetY);
document.add(img);
document.close();
}
As you can see, I create a rotated A4 page by using the rotate() method:
Document document = new Document(PageSize.A4.rotate());
I also scale the image so that it fits the page and I calculate an offset so that it is nicely centered on the page. See cardiogram.pdf:
This looks exactly the way you want it to look and you don't need to resort to using page events and changing the page dictionary.
Related
Tried many ways in the internet but my imageView still display blank of the image.
Wish to retrieve the image from url (download or refer it) but seems I missed out something.
I have activated the INTERNET permission in manifest.
icon.setImageResource(R.drawable.portrait_user);
try {
URL url = new URL("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/t1.0-1/c0.0.50.50/p50x50/1499583_10202305028778787_1063740680_n.jpg");
InputStream content = (InputStream)url.getContent();
Bitmap d = BitmapFactory.decodeStream(content);
icon.setImageBitmap(d);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Doing network I/O on the main application thread like this will crash your app on Android 4.0+.
Most likely, you will be better served using a third-party library that can download the image for you in the background and update the ImageView when it is ready. Picasso and Universal Image Loader are two of the more popular libraries for this.
I wanna set a background Template(image) in my pdf that generated by iText Library,something like this:
Click too see the image
I tried to use something like the code below:
PdfReader reader = new PdfReader("./assets/sarbargandroid.pdf");
PdfImportedPage page = writer.getImportedPage(reader, 1);
PdfContentByte cb = writer.getDirectContent();
cb.addTemplate(page, 0.0, 0.0);
but it did not work in android because .addtemplate() methode needs some awt library!
and I tried addimage stuff like this:
private void setBackground(Chapter document) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sarbarg);
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, stream);
Image img;
try {
img = Image.getInstance(stream.toByteArray());
img.setAbsolutePosition(0, 0);
document.add(img);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But it did't work correctly because the image does not fit the page and it create a pdf like this:
Click too see image
How can I do this correctly? anyone help me??
There are several things wrong with your question and that may explain why nobody answers. I'll tell you what's wrong and then maybe you can create a new question.
You say:
it did not work in android because .addtemplate() methode needs some awt library!
If the addTemplate() method needs an AWT library, then you are using iText. When working on Android, you should use iTextG: http://itextpdf.com/product/itextg
You are using PdfReader which makes people assume that you want to add a background image to an existing document. This would imply that you use PdfStamper, yet your code looks like you're using PdfWriter. That's a contradiction.
Your setBackground() method takes a Chapter as parameter to which you add an image. This is counter-intuitive:
- If you'd want to add a background image to an existing document, you'd never use the Chapter object.
- If you'd want to add a background image to every page of a PDF that is created from scratch, you'd use a page event.
Another major problem is that you create the image and add it as-is. You didn't scale it to fit the page.
Also: if you add an image in the background of an existing PDF. Parts of that image may be covered by opaque shapes that are present in the original document.
So my issue is exactly as the title states. I am downloading many different images and scaling them then setting them to a view. My issue is it seems that only .gif images are visible in the views that are created and the views that should contain .jpg images are almost blank. I say almost because there seems to be a strange tiny black dot on each .jpg view of different shapes, and by tiny I mean the size of a period, so those might be the images but reduced in size too much. Any help... p.s Though outputs I am sure the images are going though my bitmap and makeing it to the setImageBitmap().
My bitmap creating method:
#Override
protected Bitmap doInBackground(String... url) {
String url1 = url[0];
InputStream s = null;
try {
s = (new URL(url1)).openStream();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final BufferedInputStream is = new BufferedInputStream(s, 32*1024);
try {
final Options decodeBitmapOptions = new Options();
// For further memory savings, you may want to consider using this option
// decodeBitmapOptions.inPreferredConfig = Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel
if( parent.getResources().getDisplayMetrics().widthPixels >0) {
final Options decodeBoundsOptions = new Options();
decodeBoundsOptions.inJustDecodeBounds = true;
is.mark(32*1024); // 32k is probably overkill, but 8k is insufficient for some jpgs
BitmapFactory.decodeStream(is,null,decodeBoundsOptions);
is.reset();
final int originalWidth = decodeBoundsOptions.outWidth;
final int originalHeight = decodeBoundsOptions.outHeight;
Debug.out("Inbound image preview for : "+url1);
Debug.out(originalWidth);
Debug.out(originalHeight);
// inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / 2, originalHeight / 2));
}
return BitmapFactory.decodeStream(is,null,decodeBitmapOptions);
} catch( IOException e ) {
throw new RuntimeException(e); // this shouldn't happen
} finally {
try {
is.close();
} catch( IOException ignored ) {}
}
}
Also here is where I set my new image after download :
protected void onPostExecute(Bitmap map) {
//image is a object of type ImageView that has already been added to to the grand scheme of things
image.setImageBitmap(map);
}
Do I need to update the view or something? If so why do my gifs load fine?
Issue turned out to be with my scaling, particularly this line decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / 2, originalHeight / 2));
I am trying to read pdf using iText. Below is the code snippet for which i am getting exception as "not found as a file or resource":
public void readFromPdf(){
String content ="";
PdfReader reader = null;
try {
//String fileName is the string with the path to your .pdf file, for example resources/pdfs/preface.pdf
reader = new PdfReader("C:\\test\\demo.pdf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int numberOfPages = reader.getNumberOfPages();
numberOfPages = numberOfPages + 1;
for (int page = 1; page < numberOfPages; page++){
try {
String content1Page = PdfTextExtractor.getTextFromPage(reader, page);
content = content + content1Page;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Could anyone please figure out whats going wrong?
I had the same problem - nobody gave me a really good answer & also my prof didn't find a bug.. Soo.. Now I'm using PDFBox from Apache http://pdfbox.apache.org It's quite simple to read pdfs.
put your demo.pdf inside assets in your project folder and use this:
reader = new PdfReader(openAssets().open("demo.pdf"));
as long as that contstructor can accept an input stream it should work.
I am using Android Zxing library to decode a QR code, that has to be extracted in real time from a camera preview frame. The problem is that I have to use OpenCV to do the QR code detection, without asking the user to capture the image. Can anybody please tell me:
How to use frames from a camera?
How to use OpenCV to do QR detection on these frames, without capturing the image?
What algorithm to use for QR code detection?
Also, I would appreciate if somebody can tell me what functions, libraries to use, as well as some sample codes that may help me.
Update: This is what I am doing now:
Using a preview frame, decoding it to Byte array, and then passing it to RGBLuminance
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
boolean shouldCall = (System.currentTimeMillis() - lastTime) > 1000;
if (shouldCall) {
lastTime = System.currentTimeMillis();
//slow work
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Bitmap bMap1 = BitmapFactory.decodeByteArray(data, 0, data.length);
TextView textv = (TextView) findViewById(R.id.mytext);
LuminanceSource source = new RGBLuminanceSource(bMap1);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
Result result = reader.decode(bitmap);
text = result.getText();
byte[] rawBytes = result.getRawBytes();
if (rawBytes!= null)
camera.stopPreview();
BarcodeFormat format = result.getBarcodeFormat();
ResultPoint[] points = result.getResultPoints();
ParsedResult result2 = parseResult(result);
textv.setText(text);
} catch (NotFoundException e) {
camera.startPreview();
e.printStackTrace();
} catch (ChecksumException e) {
text = "Checksum Error";
camera.stopPreview();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FormatException e) {
text = "Format Error";
camera.stopPreview();
// TODO Auto-generated catch block
e.printStackTrace();
}
lastTime = System.currentTimeMillis();
}
}
});
} catch (IOException e) {
camera.startPreview();
}
}
But this isn't working. Can anybody tell me what I am doing wrong? Also, I am using the same decoding instance in my other code snippet, where I simply take a picture and decode it. But every time the picture doesn't contain a QR, the app crashes with a force close. What do I do about that? Somebody please help
Not sure, if you are using zxing already then you already have code that decodes QR codes from camera frames in Android, full stop. What else do you need -- why bother with OpenCV?