How to convert image into bit plane in android? - java

I have a project in an android platform that the process should force me to convert image into bit-plane, but I don't know how is the code.
The images are from gallery and taken from camera.
Please help.

try this:
String encoded = Base64.encodeFromFile("data/inputImage.png");
//Convert String data to binary image file
Base64.decodeToFile(encoded, "data/outputImage.png");
//Convert binary image file to byte array to base64 encoded string
FileInputStream mFileInputStream = new FileInputStream("data/inputImage.png");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] ba = bos.toByteArray();
encoded = Base64.encodeBytes(ba);
//Convert String data to binary image file
Base64.decodeToFile(encoded, "data/outputImage.png");
//Convert binary image file to base64 encoded String data file
Base64.encodeFileToFile("data/inputImage.png","data/encodedImage.txt");
//Convert base64 encoded String data file to binary image file
Base64.decodeFileToFile("data/encodedImage.txt","data/outputImage.png");
Source http://www.mysamplecode.com/2011/07/convert-image-to-string-and-string-to.html

Related

Java MimeBodyPart to byte[]

I am using Java 11. I am reading emails and retrieving their attachments using javax.mail and would like to save the attachments byte array.
So I need to convert a javax.mail.internet.MimeBodyPart (the attachment) to a byte[].
code so far:
private void savePDF(MimeBodyPart attachment, String invoiceNumber) {
byte[] data = attachment.get....
}
More info:
When I try open the PDF, I do the following:
InputStream is = response.getStream();
byte[] bytes = IOUtils.toByteArray(is);
OutputStream output = response.getOutputStream();
fos = new BufferedOutputStream(output);
fos.write(bytes);
This works perfectly when I get the bytes from a 'MultipartFile':
byte[] bytes = multipartFile.getBytes();
However, if I try use the byes from a MimeBodyPart (email attachment), it fails:
byte[] data = attachment.getInputStream().readAllBytes();
More info:
I have also tried, but I get the same error:
BASE64DecoderStream content = (BASE64DecoderStream) attachment.getContent();
byte[] bytes = content.readAllBytes();
bytes = Base64.decodeBase64(bytes);

convert raw image bytes to multipage tiff using java and vice versa

I have a requirement where i have set of base64 strings each string is one tiff file, i need to combine all these base64 strings and provide this full tiff data as base64 string, so end user can create a tiff file from the combined base64 strings.
I tried all the ways shared below but each time in my output tiff only one page is coming but i am passing 2 base64 strings.
Any inputs greatly appreciated.
Iam using this code
ArrayList al = new ArrayList();
//this is repetative so i am adding all the base64 strings to arraylist
byte[] imgBytes = Base64.decodeBase64(imageObject.get("image").toString());
al.add(imgBytes);
//writing all base64 strings to a new tiff file //here it is showing only the first page but in the top i am adding two pages.
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(toByteArray(al)));
ImageIO.write(imag, "tif", new File(processedFilesFolder,"combined.tif"));
public static byte[] toByteArray(List<byte[]> bytesList)
{
int size = 0;
for (byte[] bytes : bytesList)
{
size += bytes.length;
}
ByteBuffer byteBuffer = ByteBuffer.allocate(size);
for (byte[] bytes : bytesList)
{
byteBuffer.put(bytes);
}
return byteBuffer.array();
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
outputStream.write( data ); // this line is repetative
byte c[] = outputStream.toByteArray( );
try (OutputStream stream = new FileOutputStream("C:\\Users\\XYZ\\Desktop\\EID_Image_Desktop‌​Scan_mod.tiff")) { stream.write(c); } catch(Exception e){}
Regards,
Meerasaaheb Mohmmad

Converting encoded base 64 image string onto input stream without saving image into local file system

#/**
* This method is used to convert encoded base 64 image string into inputStream without saving it into file system
* #param : encodedBase64Image - Contains encoded base 64 image string
* #return :InputStream
*
*/#
encoded base 64 image string into inputStream without saving it into file system
InputStream getInputStream(final String encodedBase64Image) {
long mediaId
// create a buffered image
BufferedImage image = null
byte[] imageByte;
imageByte = EncodingGroovyMethods.decodeBase64(encodedBase64Image) // Coverting encoded image string into byte array
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte)
image = ImageIO.read(bis)
RenderedImage rImage = (RenderedImage) image; // forming rendered image
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(rImage, "jpg", os); // JPEG Image is formed in OutputStream
InputStream fis = new ByteArrayInputStream(os.toByteArray()); /* OutputStream into Inputstream */
return InputStream
}
encoded base 64 image string into inputStream without saving it into file system

To display a Base64 image in AngularJS

I have inserted a Base64 image to a database using this Java code:
FileInputStream mFileInputStream = new FileInputStream("C:\\basicsworkspace\\base64upload\\src\\main\\resources\\basic.png");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] ba = bos.toByteArray();
//byte[] encoded = Base64.getEncoder().encode(ba); // Util
byte[] encoded = Base64.encodeBase64(ba); // Apache
connection = DriverManager.getConnection(connectionString);
String insertSql = "INSERT INTO test (image) VALUES (?)" ;
prepsInsertProduct.setBytes(encoded);
System.out.println(insertSql);
prepsInsertProduct = connection.prepareStatement(insertSql);
System.out.println(prepsInsertProduct.execute());
But if I try to display the image in AngularJS using it, it is not displaying the image. In SQL Server I have saved my image as varbinary(max) and in AngularJS code,
config(function ($compileProvider) {
console.log($compileProvider.imgSrcSanitizationWhitelist());
})
<img src="choice:image/png;base64,{{choice.Icon}}">
But I am getting only bytes with a message like this:
unsafe:choice:image/png;base64,6956424F5277304B47676F414141414E535568455567…
Where am I going wrong? The image is in PNG format.
In POJO I made changes for byte[] and it worked. I have datatype with String and modified to byte[] but Base64 images didn't displayed but t is displaying only byte array images.
Try in your controller:
$scope.choice.Icon = 'data:image/jpeg;base64,' + yourImageData;
And in your HTML content:
<img ng-src="{{choice.Icon}}">

To read an image from Android Emulator

This is my code to convert image file into byte array.
public String GetQRCode() throws FileNotFoundException, IOException {
/*
* In this function the first part shows how to convert an image file to
* byte array. The second part of the code shows how to change byte array
* back to a image.
*/
AssetManager mgr = mAppView.getContext().getAssets();
InputStream in = mgr.open("www/Siemens_QR.jpg");
InputStreamReader isr = new InputStreamReader(in);
char[] buf = new char[20];
isr.read(buf, 0, 20);
isr.close();
// byte[] bytes = bos.toByteArray();
String abc = buf.toString();
return abc;
}
Here I am converting an image file into byte array. I am able to do this. But when try to read this image file using the path ("sdcard/Download/Siemens_QR.jpg") stored in emulator then I am getting VM aborting error. Please suggest me the correct path to read the image file stored in the emulator.
if you have jpg image stored on SD card then get the file path and try to convert the image to byte using following method...
Bitmap bitmap = BitmapFactory.decodeFile(file path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] byte_img_data = baos.toByteArray();

Categories

Resources