i couldn't find anything for my problem.
My problem is this. I have to create a pdf with a signature. I don't have problems to create pdf or to create signature but when i put them together, i resize the signature to be smaller but the quality of the signature is been reduced.
Here I show you the signature and the scaled signature.
create signature
scaled signature
This is how i create the bitmap of the signature.
mSignaturePad = (SignaturePad) dialog.findViewById(R.id.signature_pad);
Bitmap signaturePad = mSignaturePad.getSignatureBitmap();
createPDF(signaturePad);
This is how i create the canvas of the pdf page.
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
And finally i draw the bitmap after i obtain the scaled bitmap.
int width = source.getWidth();
int height = source.getHeight();
float scaleWidth = ((float) 100) / width;
float scaleHeight = ((float) 140) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(-90);
Bitmap scaledBitmap = Bitmap.createBitmap(source, 0, 0, width, height, matrix, true);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(scaledBitmap, 387, 717, paint3);
This is not the only way that i have tested to scale bitmap. I tested many ways but nothing worked. How can i scale this bitmap?
I hope you can help me. Thank you.
psd: Sorry for my english.
The below explains how to add a image from assets folder. If you have the bitmap already, then no issue just follow it from the bitmap process. you can also follow the Example.
File myFile1 = new File("/sdcard/ourdata/" + pdfname + ".pdf");
myFile1.createNewFile();
Document pdfDoc = new Document();
PdfWriter docWriter = PdfWriter.getInstance(document,
new FileOutputStream(myFile1));
document.open();
PdfContentByte cb = docWriter.getDirectContent();
InputStream inputStream = getAssets().open("signature.png");
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image signature = Image.getInstance(stream.toByteArray());
signature.setAbsolutePosition(400f, 150f);
signature.scalePercent(25f);
document.add(signature);
Related
Using iText, exactly how are objects like rectangles and stamp annotations rotated?
I've seen some examples on how to rotate annotations using current transformation matrix and using appearance matrix. You can see my implementation in the below code.
I've tried with different values for appearance matrix, it doesn't seem to be solving or fitting the image as I've expected.
My use-case is I have X, Y, width, height and rotation angle(+or-45, +or- 90 etc.,) of the location I need to fit in the image.
I'm able to rotate the image but I would like the image to fit in the dimensions I've given.
For example check this DOCUMENT , it is generated with -45 degree with rectangle height and width are 400, 250. The rotation seems ok but the image didn't fit in the dimensions I've given.
I would like to rotate the image based on the (X,Y) as axis and would really be glad if someone helped out.
public static void addStampAnnotation(File file) throws Exception
{
PdfReader reader1 = new PdfReader(file);
ByteArrayOutputStream mos = new ByteArrayOutputStream();
PdfDocument docc = new PdfDocument(reader1, new PdfWriter(mos),new StampingProperties().useAppendMode());
String filePath = "rectangle.png";
ImageData img = ImageDataFactory.create(filePath);
// img.setRotation(90);
Rectangle rect=null;
float w = img.getWidth();
float h = img.getHeight();
rect = new Rectangle(0,0, 250,400);
PdfFormXObject xObj = new PdfFormXObject(new Rectangle(w,h));
PdfCanvas canvas = new PdfCanvas(xObj, docc);
canvas.addImageAt(img,0,0,false);
// canvas.addImageWithTransformationMatrix(img, w, 0, 0, h, 0, 0);
xObj.put(PdfName.Matrix, new PdfArray(new int[]{0, 1, -1, 0, 0, 0}));
PdfStampAnnotation stamp = new PdfStampAnnotation(rect);
stamp.setNormalAppearance(xObj.getPdfObject());
int flag = PdfAnnotation.PRINT ;
stamp.setFlags(flag);
docc.getPage(1).addAnnotation(stamp);
docc.close();
file = new File("Test_Doc.pdf");
FileOutputStream fout1 = new FileOutputStream(file);
fout1.write(mos.toByteArray());
fout1.close();
}
The image used is here IMAGE
Thanks in advance !
I tried this but when I show the bitmap I can not see anything, what I want is to paint a white outline on the original image
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
public byte[] myImagetoBytes(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
byte[] bytes = myImagetoBytes();
bytes[0] = 1 & 0xff;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
this.imageView.setImageBitmap(bitmap);
Not sure what your code is supposed to do, but if you want to draw over a bitmap, you should use Canvas. You create it with its constructor:
Canvas canvas = new Canvas(bitmap);
And then you can use it like this:
Paint paint = new Paint();
paint.setColor(Color.GRAY);
RectF ovalRect = new RectF(0, 0, 10, 10);
canvas.drawOval(ovalRect, paint);
compress transparent png image to jpg give black background
I want white background instead of black
public static byte[] getBitMapBytes(Bitmap bmp, int quality) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, quality, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
fos = new FileOutputStream(f);
fos.write(getBitMapBytes(compressedImage, 60));
Got the answer :
Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(),
image.getHeight(), image.getConfig());
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(Color.WHITE);
Rect dest = new Rect(0, 0, image.width, image.height);
Rect src = new Rect(0, 0, image.width, image.height);
canvas.drawBitmap(bmp, src, dest, null);
This is a normal behavior. Jpeg doesn't manage transparency.
You can use this kind of lib if you just want to reduce the size of the image:
http://objectplanet.com/pngencoder/ or online https://tinypng.com/
I have a requirement, like drawing something in canvas and saving it to a larger image. As of now whatever I draw inside onDraw() method and save, it gives device provided image/canvas size, say something around 538(w)/852(h). I need image of almost double size, around 1000(w)/1500(h) without losing resolution. Any sample code, reference link would help definitely. Thanks in advance.
One way is to create Bitmap of desired size and draw on it using canvas:
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas canvas = new Canvas(bmp);
Finally you will have you bmp with your drawing and necessary dimentions.
EDIT::
#Override
protected void onDraw(Canvas canvas) {
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas mycanvas = new Canvas(bmp);
super.onDraw(mycanvas);
...
}
First get bitmap of your view using the function getDrawingCache(), then scale the bitmap as per your requirements.
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
//bitmap = bitmap.createScaledBitmap(..as your requirements..);
EDIT
You are saying that you want image in higher quality. You can't simply get a higher quality image from a lower quality image. However you can apply bitmap filtering to get slightly better quality.
Bitmap bitmap = getDrawingCache();
Bitmap output = Bitmap.createBitmap(/*width, height, bla bla bla*/);
Canvas canvas = new Canvas(output);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
Rect srcRect = new Rect(0,0,bitmap.getWidth(), bitmap.getHeight());
Rect destRect = new Rect(0,0,output.getWidth(), output.getHeight());
canvas.drawBitmap(bitmap, srcRect, destRect, paint);
I want to resize a .tiff file. I have used JAI toolkit to resize different types of images. Here is what I have tried to implement:
int imageWidth = 330;
int imageHeight = 490;
BufferedImage tempImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = tempImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
graphics2D.dispose();
File outfile = new File("D:/Work/YoursGallery/output.tif");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
FileSeekableStream ss = new FileSeekableStream("D:/Work/YoursGallery/sample1.tif");
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, null);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setTileSize(tempImage.getWidth(), tempImage.getHeight());
TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec.createImageEncoder("tiff", out, param);
encoder.encode(dec.decodeAsRenderedImage());
out.close();
The image created is having same size as original image has. Can anyone please tell what is the issue?
Here is the sample tiff image which I am using to test it.
http://docs.google.com/fileview?id=0BxCDhEXNFvbeMTYyMGZmNDYtODhhNy00YWI3LTkxNDgtZTNhM2FhMjg5Y2Q3&hl=en&authkey=CPCEypgM
Thanks in advance.
That's because you are writing out tempImage which is still the original image.
graphics2D.drawImage(image, 0, 0, imageWidth, imageHeight, null);
change that to:
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
or change your other code to write out image instead of tempImage
--Edit--
OK Attempt 2. Maybe having the source and destination the same is daft.
BufferedImage bsrc = ImageIO.read(new File(src));
BufferedImage bdest =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bdest.createGraphics();
AffineTransform at =
AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
(double)height/bsrc.getHeight());
g.drawRenderedImage(bsrc,at);
Try that :)
1) You are writing tempImage inside itself:
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
Should be:
graphics2D.drawImage(originalImage, 0, 0, imageWidth, imageHeight, null);
2) You are writing the image that you just read (why are You reading it btw?):
encoder.encode(dec.decodeAsRenderedImage());
Should be:
encoder.encode(tempImage);