How can print image with USB Adapter. I have error from attachment.
My code:
Bitmap bmp = getBitmapFromView(view);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
.....
.....
usbDeviceConnection.bulkTransfer(usbEndpoint,byteArray,byteArray.length,10000);
Can you help me?
Thanks.
Related
private String getBase64String() {
// give your image file url in mCurrentPhotoPath
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// In case you want to compress your image, here it's at 40%
// here i use JPEG image but now can anyone tell how can i convert any format image in String
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
You can use the Base64 Android class:
String ecodImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though.Like:
Bitmap btm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
btm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //btm is the bitmap object
byte[] b = baos.toByteArray();
When I capture using camera2 api,image is made and transfer the image to bytes next to bitmap. My purpose is to select save or not after capturing.
So It will be not made in file before Pressing save btn.
below : send side
Bitmap bitmap = textureView.getBitmap();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,bs);
byte[] byteArray = bs.toByteArray();
below : receieve side
byte[] byteArray = getIntent().getByteArrayExtra("byteArray");
bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
resultView.setImageBitmap(bitmap);
and I got received the error like below
android.os.TransactionTooLargeException
I understand the cause of error But I wanna transfer the image to another activity
Is there anyone who help this?
Put your bitmap object in Intent.putExtra("key", object),
intent.putExtra("btimap", bitmap);
Get it using Intent.getParcelableExtra("key"),
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("btimap");
Convert it to a Byte array before you add it to the intent, send it out, and decode.
//Convert to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);
Then in Activity 2:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
How to compress bitmap to jpeg? Storing in a file system is unacceptable.
Just use below code!
Bitmap bmp = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
I am aware of the code below
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
but the issue is that this code reduces the image quality so much that it is unrecognizable how can i avoid
photo.compress(Bitmap.CompressFormat.PNG,100, stream);
You can use CompressFormat.PNG, where the compress quality doesn't matter, its always lossless as described in the documentation
array = new byte[w*h*4];
Buffer dst = ByteBuffer.wrap(array);
bmp.copyPixelsToBuffer(dst);
I would like to get a byte array from an jpeg image located in my res/drawable file ?
Does anyone know how to do that please ?
Drawable drawable;
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Get a bitmap decodeResource(android.content.res.Resources, int)
Then either compress it to ByteArrayOutputStream() or copyPixelsToBuffer and get your array from the buffer.
http://developer.android.com/reference/android/graphics/Bitmap.html
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mPhoto.compress(Bitmap.CompressFormat.JPEG /* FileType */,
100 /* Ratio */, stream);
HTH !