I need to convert bitmap to binary image for my hw.Do u know anything about that?
Are you looking for an algorithm to perform the conversion?
The easiest way is to compare each pixel value with a fixed threshold: if the pixel value is less than the threshold, the corresponding output pixel is black (0), else it is white (1).
If you wish to determine the threshold automatically, you may want to implement Otsu's method. That method does a correct job overall when you can't make too many assumptions about the pixels distribution in your image.
http://en.wikipedia.org/wiki/Otsu%27s_Method
As a reference, that's how it looks like in Mathematica:
Binarize[image, threshold], and Binarize[img] for Otsu's method.
You can use the bitmap convert function and write it to an output stream and then use the output stream to get for yourself the byte array
I hope that helps
you can look this link converting Java bitmap to byte array, it can convert bitmap to binary, can then you should look at display image from byteArray
Hope this helps...
Bitmap bitmapObtained =//get your bitmap
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapObtained.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
perhaps this is your code
imageID = cursor.getString(columnIndex);
// uri = Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, "" + imageID);
Log.v("dklfdlk",imageID);
bitmap = BitmapFactory.decodeFile(imageID);
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 78, 78, true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
try this
Related
I have an image which I turn into a byte array. I then encrypt this byte array with AES and I want a visual result to display representing this encryption.
The problem is all the header and meta information is also encrypted so this encrypted byte array, which is passed to byteToImage(), is not recognized as a valid representation of an image i.e. decodeByteArray() returns null.
I have tried cutting off the first 512 bytes of the original image and appending that back on to the start of the encrypted byte array in the hope that this will restore the header information - but it hasn't worked. I have this with .png and .bmp images. What I ideally want is a way to represent a RAW image in android and encrypt this information byte for byte - leaving out the need to fiddle around with headers etc.
I would really appreciate any help.
private static byte[] imageToBytes(ImageView iv){
byte[] imageInByte = null;
Bitmap originalImage;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
originalImage = drawable.getBitmap();
originalImage.compress(Bitmap.CompressFormat.PNG, 0, baos); // was 70
imageInByte = baos.toByteArray();
return imageInByte;
}
private static Bitmap bytesToImage(byte data[]) {
// byte[] x = Base64.decode(data, Base64.DEFAULT); using x in place of data also fails
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
return bmp;
}
Your idea will not work. If you encrypt just a lot of bytes of a bitmap byte array you will demolish its structure and after that you don't have a bitmap anymore. So you cannot display it. If you want to be able to display the 'encrypted' image then do it pixel wise. Have a look at Bitmap.getPixel()/setPixel() or Bitmap.getPixels()/setPixels().
I have been trying to pass a single byte array (compressed bitmap) from one activity to another. When I attempt to decode the byte array back to a bitmap, and show the bitmap, it appears to be a completely transparent bitmap.
I use this code on the first activity to compress the bitmap and send the byte array:
try {
InputStream selectedImage = getContentResolver().openInputStream(Uri.parse(photoPath));
bitmap = BitmapFactory.decodeStream(selectedImage);
} catch (FileNotFoundException exception) {
Log.e(this.toString(), exception.toString());
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte [] byteArray = stream.toByteArray();
Log.e("PANTHERIT_BYTEARRAY", byteArray.toString());
Intent stickerActivity = new Intent (this, StickerActivity.class);
stickerActivity.putExtra("byteArray", byteArray);
startActivity(stickerActivity);
And I use this code in the accepting activity to decompress and store the bitmap:
byte [] byteArray = getIntent().getByteArrayExtra("byteArray");
Log.e("STICKER_BYTEARRAY", byteArray.toString());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);
ImageView bitmapview = (ImageView) findViewById(R.id.bitmap_view);
bitmapview.setImageBitmap(bitmap);
I copied the code to the first activity, compressed the bitmap and immediately decompressed it. I took the decompressed bitmap and set it to an ImageView in that activity and it worked fine. So the error seems to be in the passing of the byte array from the first activity to the second, but I cannot figure out why that would be.
I found an answer. I do not really know why the byte array was getting messed up, maybe it was too large? Even so, both the input and output byte arrays were the same length, so not a very good explanation.
Anyway, I found my answer here: Send Bitmap Using Intent Android. The first answer by Zaid Daghestani is the same as what I initially tried, but his edit is what worked. Instead of passing a byte array, you save the compressed bitmap to a temporary file, and pass the filename through the intent. Definitely works for passing a bitmap, now I just have to figure out how to display the bitmap on a SurfaceView instead of an ImageView...
Also, just in case you need a mutable bitmap (in order to pass it to an ImageView) you can use:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bitmap = BitmapFactory.decodeStream(input, null, options);
I'm getting direct stream from camera and I need to save a Bitmap into a ByteBuffer and recover it. Here is my code:
YuvImage yuv = new YuvImage(data.getExtractImageData(), previewFormat, width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] bytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap imageResult = RotateImage(image, 4 - rotation);
imageResult = cropBitmap(imageResult, data.getRect());
int nBytes = imageResult.getByteCount();
ByteBuffer buffer = ByteBuffer.allocateDirect(nBytes);
imageResult.copyPixelsToBuffer(buffer);
return buffer.array();
Code to convert byte[] back to Bitmap:
Bitmap bitmap = BitmapFactory.decodeByteArray(images.getImage(), 0, images.getImage().length);
But then, bitmap is Null after conversion...
Any idea on what's wrong?
To clarify: I need to save the byte[] image in the Native memory, that's why I do a ByteBuffer.allocateDirect. I need to crop the image in a specific point, that's why I need the bitmap.
decodeByteArray() decodes a compressed image (e.g. a JPEG or PNG) stored in a byte array. However copyPixelsToBuffer() copies the contents of a Bitmap into a byte buffer "as is" (i.e. uncompressed), so it can't be decoded by decodeByteArray().
If you don't want to re-encode your bitmap, you can use copyPixelsToBuffer() like you are doing, and change your second code block to use copyPixelsFromBuffer() instead of decodeByteArray().
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(images.getImage()));
You'll need to save the width and height. Also make sure the Bitmap.Config is the same.
Basically, if you save it compressed then you have to load it compressed, and if you save it uncompressed then you have to load it uncompressed.
You should also set the byte order when you allocate the buffer because Java is big endian, the buffer is therefore big endian by default, android is little endian and the underlying cpu architecture can vary but is mostly little endian wrt android:
buffer.order( ByteOrder.nativeOrder() );
i'm trying to draw a Bitmap but only with a selected area from the original Bitmap or Imageview. The requirement is that the final picture must show only 1/3 from the top of the original Bitmap. I attach a draft. I suppose that i should use canvas, but i do not know exactly how it works.
Thanks in advance!!
Bitmap getTheReducedBitmap(Bitmap fullLengthBitnap)
{
Bitmap backDrop=Bitmap.createBitmap(fullLengthBitnap.getWidth(), fullLengthBitnap.getHeight()/3, Bitmap.Config.RGB_565);
Canvas can = new Canvas(backDrop);
can.drawBitmap(fullLengthBitnap, 0, 0, null);
return backDrop;
}
This is the documentation for the method you should use.
private void draw(Canvas c, Bitmap bmp){
Rect r=new Rect(0,0,bmp.width,bmp.height/3);
Rect drawR=new Rect(0,0,c.width,c.height/3);
c.drawBitmap(bmp,r,drawR,null);
}
or as a one liner:
c.drawBitmap(bmp,new Rect(0,0,bmp.width,bmp.height/3),new Rect(0,0,c.width,c.height/3),null);
It allows you to specify where on the canvas you want to draw it too, and where you want the segment to be from.
#Eu.Dr. 's answer would not work if you wanted to draw anything else on the canvas below it.
val newBitmap=Bimtap
.createBitmap(your_view.width,your_view.height,Bitmap.Config.ALPHA_8)
//note: for tablet mode your_view.width,height will increase drastically so
//you might want
//to fix the size of drawing area for optimization
//ALPHA_8 each pixel requires 1 byte of memory.
//RGB_565 Each pixel is stored on 2 bytes
//ARGB_8888 Each pixel is stored on 4 bytes
//after this you can further apply the compress like
[Refer more from google][1]
val stream = ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
val byteArray = stream.toByteArray(); // convert drawing photo to byte array
// save it in your internal storage.
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES
+"attendance_sheet.png");
try{
val fo = FileOutputStream(storageDir);
fo.write(byteArray);
fo.flush();
fo.close();
}catch(Exception ex){
}
I've converted an bitmap image into string to save it:
............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Then I retrieve the bitmap from string to set an activity's background just like that:
byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);
Everything works fine but the image quality reduces tremendously. How can I keep the image quality same as the original image? Did I do something wrong here that have reduced the quality?
JPEG is lossy, no matter what quality settings you use. If you want to keep the image unchanged, you have to use lossless compression. for example Bitmap.CompressFormat.PNG
You are having here a tradeoff situation between picture quality and memory usage. Take a look at this line:
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
photo.compress is obviously decreasing your image resolution in a factor given by the second parameter, unfortunately, this is the best quality you can get, since between 0 - 100, 100 stands for the best quality you can get. Now, you have another option, depending on the original picture's size you can just save the image without compressing it, but be aware that most cases this doesn't work and Jalvik can throw an OutofMemoryException,
hope this helps.