Convert Java Code of Bitmap into C# code In Console app - java

I am trying to convert Java code into c#
So here is Java code
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap decodeStream = BitmapFactory.decodeStream(openInputStream, null, options);
Then I am saving this bitmap by
File appDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dest = new File(appDirectory, "yourImage.jpg");
try (FileOutputStream out = new FileOutputStream(dest)) {
decodeStream.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
} catch (IOException e) {
e.printStackTrace();
}
Now can anyone help me to convert this code into c#. I Tried to import Xamrine Android DLL but got success no far.
I am

As far as I understand you only need to load simple jpg image. That is all your java code do.
If you want to load jpg image from stream you can use
Bitmap.FromStream()
e.g.
using (FileStream fs = new FileStream(#"Image Address.jpg", FileMode.Open, FileAccess.Read))
{
var decodeStream = Bitmap.FromStream(fs);
}
of course you can open your image without stream too.
var image = Bitmap.FromFile(#"Image Address.jpg");
So your code will be something like this
File appDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dest = new File(appDirectory, "yourImage.jpg");
var image = Bitmap.FromFile(dest.FullName);
You can not access Bitmap class out of the box.
For dot net core install package System.Drawing.Common.
For dot net framework add reference to System.Drawing.
For Xamarin see this answer https://stackoverflow.com/a/34869330/5964792

Related

How to compress downloaded images and decompress when needed in Android?

How to compress jpg/bmp files which I can store in the memory then when needed decompress those images and show to users without losing too much of image quality? How to do the compress and decompress, any guidance/ link would be helpful.
Thank you
create image thumbnails
'byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
}
catch(Exception ex) {
}`
if u need for thumbNail then use
ThumbNailUtils.extractThumbnail(Bitmap source,int width,int height)
it show thumbnail from bitmap and when u want to show original bitmap then show bitmap
.
use wisely bitmap object cause it take more memory at runtime.

BufferedImageOp Kernel Convolution not doing anything

I am attempting to perform a basic kernel convolution pass on an image using the BufferedImageOp package in java.awt.image. This is the code I have:
BufferedImage img = null;
File f = null;
//read image
try {
f = new File("keys.JPG");
img = ImageIO.read(f);
} catch (IOException e) {
System.out.println(e);
}
float[] gaussian = {
1/16f, 1/8f, 1/16f,
1/8f, 1/4f, 1/8f,
1/16f, 1/8f, 1/16f,
};
BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, gaussian));
BufferedImage dest = op.filter(img, null);
File outputfile = new File("image.jpg");
ImageIO.write(dest, "jpg", outputfile);
My code attempts to load the image keys.JPG and then convolve this image with the Gaussian blur kernel and save the image to the file image.jpg. When I run the code, it processes for a bit then terminates and saves the image successfully but when I compare the original and the new images, they are identical.
Looking online at some code examples, my code should work. Am I missing something?
Thanks
As #haraldK mentioned, my image was too large to notice a difference. The code works as expected.

Download .svg or .xml to internal memory and show it in ImageView

I need to download vector images from server and display them in ImageViews. I succesfully downloaded the images to external storage, but now I'm having a problem displaying them.
I'm using a SVG library from larvalabs and here's the code:
File image = new File(Environment.getExternalStorageDirectory(), "image.svg");
if (image.exists()) {
try {
InputStream imageStream = new FileInputStream(image);
SVGBuilder builder = new SVGBuilder();
builder.readFromInputStream(imageStream);
SVG svg = builder.build();
Drawable draw = svg.getDrawable();
((ImageButton) view).setImageDrawable(draw);
imageStream.close();
} catch (Exception e) {...
It turns out that the InputStream is empty. But the file is there, the path shows to the correct directory, the file is as it should be, so I don't know what else could be the problem. I'm adding a print screen from debugger if it helps.
Debugger Print Screen

load picture from android phone into byte[]

I want to load a picture i have in the smartphone so i can than send it over the internet to a webservice i created.
Here i provide a sample code of what i am trying and not working.
Bitmap bm = BitmapFactory.decodeFile(path);
System.out.println("BITMAP: "+bm != null);
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bm.getWidth() *bm.getHeight());
bm.compress(CompressFormat.JPEG, 100, buffer);
I made sure that bm isn't null with the system out print. I get a NullPointerException in ByteArrayOutputStream. Any suggestions?
Try this. Use file name with the path
String[] files = null;
File path = new File(Environment.getExternalStorageDirectory(),"folder path");
if(path.exists())
{
filename = path.list();
}
for(int i=0; i<count;i++)
{
Bitmap bitmapOrg = BitmapFactory.decodeFile(path.getPath()+"/"+ files[i]);
}

how to decode a qrcode using zxing through webcam

i have a code to decode a qrcode that take a image file and decode it. am using zxing library. but how can i make this capture qrcode from webcam and decode it. what are the changes i need to do?? can any one plz explain this step by step.
here is the code:
public class QrCodeDecoder
{
public String decode(File imageFile)
{
BufferedImage image;
try
{
image = ImageIO.read(imageFile);
}
catch (IOException e1)
{
return "io outch";
}
// creating luminance source
LuminanceSource lumSource = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lumSource));
// barcode decoding
QRCodeReader reader = new QRCodeReader();
Result result = null;
try {
result = reader.decode(bitmap);
}
catch (ReaderException e)
{
return "reader error";
}
return result.getText();
}
}
You are pretty far away from a solution, I don't think anyone can explain it "step by step". You will first need to find a library that is capable of grabbing an image from a webcam. You might start with this library, though I'm sure there are others.
Once you can capture an image, start to figure out if it is in a format that the above code expects. If you are lucky, it will be, if not you will have to convert between the two formats.

Categories