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);
Related
I Convert Text to Bitmap with this method:
private Bitmap textToBitmap(String text)
{
Bitmap myBitmap = Bitmap.createBitmap(400, 50, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface clock = Typeface.createFromAsset(context.getAssets(),"fonts/myfont.ttf");
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(clock);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE );
paint.setTextSize(38);
paint.setTextAlign(Paint.Align.RIGHT);
myCanvas.drawText(text, 400, 30, paint);
return myBitmap;
}
But in some of phone my bitmap is very small and in other its very Big
why that Happen?
Bitmap myBitmap = Bitmap.createBitmap(400, 50, Bitmap.Config.ARGB_4444);//its unit is px, practical size of 1 px is different in various device.
You should resize bitmap according to device scaledDensity, here is a good solution
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 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);
I have an image which is stored as a byte[] array, and I want to flip the image before I send it off to be processed elsewhere (as a byte[] array).
I've searched around and can't find a simple solution without manipulating each bit in the byte[] array.
What about converting the byte array[] to an image type of some sort, flipping that using an existing flip method, and then converting that back to a byte[] array?
Any advice?
Cheers!
Byte array to bitmap:
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Use this to rotate the image by providing the right angle (180):
public Bitmap rotateImage(int angle, Bitmap bitmapSrc) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmapSrc, 0, 0,
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}
Then back to the array:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] flippedImageByteArray = stream.toByteArray();
The following is a method used to flip the image which is stored as byte array and return the result in byte array.
private byte[] flipImage(byte[] data, int flip) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
switch (flip){
case 1: matrix.preScale(1.0f, -1.0f); break; //flip vertical
case 2: matrix.preScale(-1.0f, 1.0f); break; //flip horizontal
default: matrix.preScale(1.0f, 1.0f); //No flip
}
Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp2.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
If you want a vertical flipped image then pass 1 as flip value and for horizontal flip pass 2.
For Eg:
#Override
public void onPictureTaken(byte[] data, Camera camera) {
byte[] verticalFlippedImage = flipImage(data,1);
byte[] horizontalFlippedImage = flipImage(data,2);
}
I have the following code for this purpose in my app but it does nothing, and halts the application:
bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length));
Try like this
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
Then I suppose you draw the image too like this:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bmImage, 0, 0, paint);
use below line to convert bytes into Bitmap, it is working for me.
Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
you need to put above line outside of loop, as it takes Bytes Array and convert into Bitmap.
P.S. :- here imageData is bytes array of Image
Try this :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 95, blob);
byte[] bitmapdata = blob.toByteArray();
Bitmap bmp = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
ImageView image = (ImageView) findViewById(R.id.ivPhoto);
image.setImageBitmap(bmp);