I am trying to create a camera intent and a sub portion of the code is given below.
public void onPictureTaken(byte[] data, Camera camera) {
String dat = new String(data);
byte[] datas = dat.getBytes();
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
Bitmap bMap = BitmapFactory.decodeByteArray(datas, 0, datas.length);
iv2.setImageBitmap(bMap);
}
This keeps the imageview blank, however when I give
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
the imageview is properly loaded. Am I doing any mistake in byte array to string conversion??
why do you need these two lines?
String dat = new String(data);
byte[] datas = dat.getBytes();
use data directly in the decodeByteArray
You dont even need to convert the byte[] to String. Just use it as it is.
public void onPictureTaken(byte[] data, Camera camera) {
preview.setVisibility(View.GONE);
ImageView iv2 = (ImageView)findViewById(R.id.iv1);
// ensure ImageView is visible.
iv2.setVisibility( View.VISIBLE);
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
iv2.setImageBitmap(bMap);
}
Just use the above modified code.
Related
After implementing Camera2 Api for clicking image I am getting image in this listener
private ImageReader.OnImageAvailableListener imageAvailableListener = new ImageReader.OnImageAvailableListener() {
#Override
public void onImageAvailable(ImageReader reader) {
Image img = reader.acquireLatestImage();
final Image.Plane[] planes = img.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
byte[] byteArray = new byte[buffer.remaining()];
buffer.get(byteArray);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
uri = getImageUri(getContext(), bmp);
}
};
I tried to parse the Image to Bitmap in order to get the image uri using this method
private Uri getImageUri(Context context, Bitmap inImage) {
this.saveToInternalStorage(inImage);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "CAPTURE" + captureCount, null);
return Uri.parse(path);
}
But the image uri, which I get from above method, when I try to use it for displaying image in ImageView doesnot shows up any thing but I can see the clicked image in gallery.
Image uri which I get starts like external/**/<some number>
What I am trying to do is I want to get uri of clicked image and use
that uri to edit it.
If there is any other way to edit image may be like using bitmap or some other form pls do mention.
I have an ImageView object acquired from the .xml file:
mCameraView = (ImageView) findViewById(R.id.camera_view);
I record an image taken from the camera many times a second. For each new image frame the following method gets called.
#Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
mCameraView.somehowDisplay(image); // HOW?
}
I want place the image into the image view: How to do that?
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
mCameraView.setImageBitmap(myBitmap);
This should work.
Reference Answer
I know, this is an old question, but I've got problems with encoding a byte[] into a bitmap...
Background: I'm writing an Andoid-App which receives picturebytes via UDP, encodes them into a bitmap and displays the picture in an image view.
Since my functions didn't work, I cancelled the UDP-Connection for testing and wrote all the image-bytes in a huge variable. So they're all correct...
The function returns "null".
The function I'm using:
public Bitmap ByteArrayToImage(byte[] imageData)
{
var bmpOutput = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
return bmpOutput;
}
another function I tried out:
public Bitmap ByteArrayToImage2(byte[] imageData)
{
Bitmap bmpReturn;
bmpReturn = (Android.Graphics.Bitmap) Android.Graphics.Bitmap.FromArray<byte>(imageData);
return bmpReturn;
}
A function I found in the internet:
public static Bitmap bytesToUIImage (byte[] bytes)
{
if (bytes == null)
return null;
Bitmap bitmap;
var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
//Create a folder for the images if not exists
System.IO.Directory.CreateDirectory(System.IO.Path.Combine (documentsFolder, "images"));
string imatge = System.IO.Path.Combine (documents, "images", "image.jpg");
System.IO.File.WriteAllBytes(imatge, bytes.Concat(new Byte[]{(byte)0xD9}).ToArray());
bitmap = BitmapFactory.DecodeFile(imatge);
return bitmap;
}
Most unfortunately, the last function didn't work as well, but here I have do admit, that I was a bit confused about the 'documents' in
string imatge = System.IO.Path.Combine (documents, "images", "image.jpg");
I got an error and changed it into documentsFolder since i guess, that should (or could) be right....
Thank you in advance for your help
it seems, I found the error...
I stored the public Bitmap ByteArrayToImage(byte[] imageData) in another class. I don't know why, but when I decode the Bytearray in the class that also receives the array, all works fine...
If someone knows the reason, feel welcome to let me know, but for now I'm happy ;-)
I did something similar
On sender side:
Camera.Parameters parameters = camera.getParameters();
if (parameters.getPreviewFormat() == ImageFormat.NV21) {
Rect rect = new Rect(0, 0, parameters.getPreviewSize().width, parameters.getPreviewSize().height);
YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, parameters.getPreviewSize().width, parameters.getPreviewSize().height, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
yuvimage.compressToJpeg(rect, 75, os);
byte[] videoFrame = os.toByteArray();
//send the video frame to reciever
}
On receiving side:
DataInputStream dIn = new DataInputStream(socket.getInputStream());
int length = 0;
length = dIn.readInt();
if (length > 0) {
byte[] message = new byte[length];
dIn.readFully(message, 0, message.length);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
final Bitmap bitmap = BitmapFactory.decodeByteArray(message, 0, message.length, options);
ReceiverActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
imgPreview.setImageBitmap(bitmap);
}
});
There is a built in method to decode a byte array into a bitmap. The problem comes when we are talking of big images. With small ones you can use:
Bitmap bmp = BitmapFactory.DecodeByteArray (data, 0, data.length);
Be aware. Those bitmaps are not mutable, so you will not be able to use canvases on those. To make them mutable go to: BitmapFactory.decodeResource returns a mutable Bitmap in Android 2.2 and an immutable Bitmap in Android 1.6
I am not able to convert a byte array to a Bitmap in the preview callback method. I'm using the following code:
public void onPreviewFrame(byte[] data, Camera camera) {
Bitmap bmp = BitmapFactory.decodeByteArray(data , 0, data.length);
Mat orig = new Mat(bmp.getHeight(),bmp.getWidth(),CvType.CV_8UC3);
Bitmap myBitmap32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, orig);
}
bmp is null. Please justify how to convert bitmap image.
I am making an application in Android, which requires sending bitmap object from one activity and displaying that bitmap object sent on the second activity page. But , I am getting a blank screen
Here is a sample of my code for sending the Bitmap object :-
Intent intent = new Intent(Display2.this, Display3.class);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
intent.putExtra("BitmapImage", bitmap);
startActivity(intent);
Now the part of code on second activity to retrieve the sent object and displaying it on screen :-
public class Display3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BitmapImage");
ImageView myIV = (ImageView) findViewById(R.id.imageView1);
bitmap = bitmap.createBitmap(myIV.getWidth(), myIV.getHeight(), Bitmap.Config.RGB_565);
myIV.setImageBitmap(bitmap);
setContentView(R.layout.display3);
}
}
can anyone suggest whats wrong in this part ?
Thanks !
Bitmap implements Parcelable object, so you could always pass it in the intent like below :
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
this is not good for passing bitmap from one activity to another..
You can simply name you Bitmap as static first.
then create a method like
public static Bitmap getBitmap(){
return bitmap;
}
then you can simply call from other activities,
bitmapexistingclass.getBitmap();
if we use intents for passing bitmap we will get some errors check this question How to pass bitmap from one activity to another
from your code above you are creating an empty bitmap:
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
I do not see any reason why from this code you'll get anything else then blank black bitmap with hight and width equals to the imageView hight and width
what you are expecting from android to fill drawing in Bitmap itself , you are creating an Empty Bitmap Object with height and width equavalent to ImageView object;
as Bitmap implements Parcelable you can simply put it in bundle and retrive on other Activity
Use below two methods to convert a bitmap to string and vice versa. Then you can pass the string with intents from activity to another activity. I too do the same in my application.
Then we dont need to use ParcelableExtra and serialzable class.
public String convertBitmapToString(Bitmap src) {
if(src!= null){
ByteArrayOutputStream os=new ByteArrayOutputStream();
src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os);
byte[] byteArray = os.toByteArray();
return Base64.encodeToString(byteArray,Base64.DEFAULT);
}
return null;
}
public Bitmap getBitMapFromString(String src){
Bitmap bitmap = null;
if(src!= null){
byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
return bitmap;
}
return null;
}