Process Images and resize them on a Ktor server - kotlin

I am trying to receive an image using a Ktor server, and process it, all the Android libraries aren't working as they use things like BitmapFactory, java.awt.* , Buffered images and etc.
Does Ktor have any set of tools to process images?
What I am trying to achieve is to receive an image and resize it.

Ktor doesn't have anything specific to process the images. Because it doesn't have to.
BufferedImage is not an Android library. It's part of java.awt, and you can use it in Ktor code. Same for javax.imageio.ImageIO. The only part you can't use is BitmapFactory, since it is part of Android SDK.
As of the part how to get the initial upload (since you're using Ktor, that's what you want, probably), you can look here: https://ktor.io/servers/uploads.html
This should work on Kotlin JVM:
import javax.imageio.*
import java.io.*
fun main() {
val image = ImageIO.read(File(""))
}

1 - Receive an image file : Like Alexey Soshin pointed out, use whatever suits your needs to read/get the image
2 - Process (resize the image) : using a library such as thumbnailator
see example:

Related

how to convert BitMatrix to Painter/ImageBitmap/ImageVector without the use of android libraries? (for desktop application)

I am trying to generate a QR code within my desktop app. So, for the QRcode image, I am generating a BitMatrix of the QRCode. I am using the zxing library for this purpose since I didn't find anything else for the same.
BitMatrix generator -
import com.google.zxing.BarcodeFormat
import com.google.zxing.common.BitMatrix
import com.google.zxing.qrcode.QRCodeWriter
fun getMatrix(text: String, size: Int): BitMatrix {
return QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size)
}
The problem I am encountering is that majority of the solutions are the same and they use this method
Bitmap.createBitmap(..., ..., ...)
This method uses the android libraries which aren't available when building a desktop app. So due to this reason, I haven't gotten a single correct answer on how to do this. All I want is how to convert a given text to a QR code which I can show in my desktop app. Also, latency isn't an issue. So, If you have a solution, do suggest without hesitation.

Cannot find the exact eror sometimes showing Adapter not attached sometimes cannot find memetrack [duplicate]

I'm running Ubuntu 16.04. And on Android Studio when I try to run my application in the emulator I get the following error:
FATAL EXCEPTION: main
Process: project name here, PID: 2528
java.lang.RuntimeException: Canvas: trying to draw too large(216090000bytes) bitmap.
at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:528)
at android.widget.ImageView.onDraw(ImageView.java:1316)
at android.view.View.draw(View.java:17185)
at android.view.View.updateDisplayListIfDirty(View.java:16167)
at android.view.View.draw(View.java:16951)
at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
at android.view.View.updateDisplayListIfDirty(View.java:16162)
at android.view.View.draw(View.java:16951)
at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at
etc...
I did have to run through some hoops to get my emulator working however, needed to create a sym-link so I can run the emulator on AMD. Not sure if this is part of the problem. And for the life of me I cannot figure why it continues to do this. In my group there are others who emulate the project just fine on the same emulated phone and SDK.
Move your image in the (hi-res) drawable to drawable-xxhdpi. But in app development, you do not need to use large image. It will increase your APK file size.
The solution is to move the image from drawable/ folder to drawable-xxhdpi/ folder, as also others have mentioned.
But it is important to also understand why this somewhat weird suggestion actually helps:
The reason is that the drawable/ folder exists from early versions of android and is equivalent to drawable-mdpi. When an image that is only in drawable/ folder is used on xxhdpi device, the potentially already big image is upscaled by a factor of 3, which can then in some cases cause the image's memory footprint to explode.
This solution worked for me.
Add these lines in your Manifest application tag
android:largeHeap="true"
android:hardwareAccelerated="false"
I had the same problem.
If you try to upload an image that is too large on some low resolution devices, the app will collapse.
You can make several images of different sizes (hdpi, xxdpi and more) or simply use an external library to load images that solve the problem quickly and efficiently.
I used Glide library (you can use another library like Picasso).
panel_IMG_back = (ImageView) findViewById(R.id.panel_IMG_back);
Glide
.with(this)
.load(MyViewUtils.getImage(R.drawable.wallpaper)
.into(panel_IMG_back);
This issue can be resolved by 3 methods as follows:
Method 1:
By adding image into a res/drawable-nodpi folder (By doing this it will not pre-scale your image).
Method 2:
Generate all dpi(hdpi,ldpi,mdpi,xhdpi,xxhdpi,xxxhdpi) of image and add to drawable folder. (This process will increase APK size).
Method 3:
Add image to drawable/drawable-xxhdpi folder.
All these methods are verified.
Turns out the problem was the main image that we used on our app at the time. The actual size of the image was too large, so we compressed it. Then it worked like a charm, no loss in quality and the app ran fine on the emulator.
For this error was like others said a big image(1800px X 900px) which was in drawable directory, I edited the image and reduced the size proportionally using photoshop and it worked...!!
If you don't want your image to be pre-scaled you can move it to the res/drawable-nodpi/ folder.
More info: https://developer.android.com/training/multiscreen/screendensities#DensityConsiderations
if you use Picasso change to Glide like this.
Remove picasso
Picasso.get().load(Uri.parse("url")).into(imageView)
Change Glide
Glide.with(context).load("url").into(imageView)
More efficient Glide than Picasso draw to large bitmap
I also had this issue when i was trying to add a splash screen to the android app through the launch_backgrgound.xml . the issue was the resolution. it was too high so the images memory footprint exploded and caused the app to crash hence the reason for this error. so just resize your image using a site called nativescript image builder so i got the ldpi,mdpi and all the rest and it worked fine for me.
I just created directory drawable-xhdpi(You can change it according to your need) and copy pasted all the images to that directory.
This can be an issue with Glide. Use this while you are trying to load to many images and some of them are very large:
Glide.load("your image path")
.transform(
new MultiTransformation<>(
new CenterCrop(),
new RoundedCorners(
holder.imgCompanyLogo.getResources()
.getDimensionPixelSize(R.dimen._2sdp)
)
)
)
.error(R.drawable.ic_nfs_default)
.into(holder.imgCompanyLogo);
}
Try using an xml or a vector asset instead of a jpg or png.
The reason is quite obvious in the exception name itself i.e. the resolution of the resource is too large to render.
You can png to xml using online tools like https://svg2vector.com/ OR add your image to drawable-xxhdpi folder.
Solution for Picasso is add Transformation for resize image.
class ResizeTransformation(private val maxSize: Int) : Transformation {
override fun transform(source: Bitmap?): Bitmap? {
var result:Bitmap? = null
if (source != null) {
var width = source.width
var height = source.height
val bitmapRatio = width.toFloat() / height.toFloat()
if (bitmapRatio > 1) {
width = maxSize;
height = (width / bitmapRatio).toInt()
} else {
height = maxSize;
width = (height * bitmapRatio).toInt()
}
result = Bitmap.createScaledBitmap(source, width, height, true)
source.recycle()
}
return result
}
override fun key() = "resize()"
}
Use:
Picasso.get()
.load(url)
.transform(ResizeTransformation(2400)) //FHD+ resolution
.into(view)
Convert your all png formats into webs format. You can do it by Android Studio.

abc bytecode decoding failed

I am working on a project in Flex 3.
First off, I should explain I'm learning Flex as I go along. I was tasked with figuring out something and I needed Flex to do it as that is what our UI guys do (I am a database developer and work in PL/SQL and minimal ColdFusion).
What I'm trying to do is create a PDF, and I was able to get this to work on my local machine with a simple program using Flash Builder 4.6. It grabs a snapshot of an object and turns it into a PDF with no issues.
When I move this to Flex 3, I get issues. The error I am getting is:
abc bytecode decoding failed.
I tried to refresh and clean the project per some google searches suggestions, but that did not fix anything.
My code looks like this:
public function pdfBtn_printPDF(event:MouseEvent):void
{
Alert.show("Hello!");
}
Starting with an alert box to verify the function works perfectly. With my full function code or even if I replace it with just the following:
public function pdfBtn_printPDF(event:MouseEvent):void
{
var createPDF:PDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
}
I get the error above and my project won't work. I am at a loss as to what I am missing. am I creating the variable wrong? If I create it outside of the function it still gives me the decoding error and I have verified that I am creating the variables like other variables inside this project.Any help would be appreciated!!
The issue was I had compiled a library in Flash 4.6 and it wasn't compatible with Flex 3. It is working now with a new library.

Sharing StorageItems from a byte[] array

I want to implement the Share source contract in my WinRT C# Metro app (Windows Release Preview). My app is storing arbitrary files. Not in the filesystem, but instead I get the data over a WCF service as byte[]. Now I want to share such "files" in my app.
The only possibility I've seen with a standard data format is using the SetStorageItems() method on the DataPackage. Thus I'm facing the challenge to convert the data from my byte array to a StorageFile, which can be shared. I found the StorageFile.CreateStreamedFileAsync() method and wanted to use it in this way:
// filename: string
// fileContent: byte[]
// ... setting DataPackage title and description ...
DataRequestDeferral deferral = args.Request.GetDeferral();
var file = await Windows.Storage.StorageFile.CreateStreamedFileAsync(filename,
async stream => await stream.WriteAsync(fileContent.AsBuffer()), null);
args.Request.Data.SetStorageItems(new List<IStorageItem> { file });
deferral.Complete();
It compiles fine, but it doesn't work as expected. I've tried the sharing with the standard Mail app. The Mail share view opens and I can create a new mail. The file is shown without thumbnail (as expected), but the e-mail can't be sent. It's showing the sending progress for several minutes and then an error occurs: "Couldn't share {filename} with Mail.". The share charm shows "Something went wrong" and "[...] Mail can't share right now. Try again later.".
It works perfectly when I load the StorageFile from the file system: the mail opens and is sent within seconds, no problems here. So either I'm using CreateStreamedFileAsync() wrong or there's a bug in this method, what do you think?
In the callback passed into CreateStreamedFileAsync, you need to actually dispose of the object - that signals to the OS that you are done.
Wrote a complete example here
The Mail app is not a target for sharing files. From http://blogs.msdn.com/b/b8/archive/2012/06/14/building-the-mail-app.aspx: "Mail supports sharing text, links, and pictures."
Remember that there are 2 parts of the Share contract: Share sources and Share targets. As you know, there are many different data formats that can be shared between them, like text, pictures, URIs, and files. The full list of the different data formats that are supported is at http://msdn.microsoft.com/en-us/library/windows/apps/hh771179.aspx.
I recommend that you use the Share Target Sample app to test that your file is being shared properly - share to this and it will display everything that is being shared from your app as a source (and it does accept files for sharing). You can download it from http://code.msdn.microsoft.com/windowsapps/Sharing-Content-Target-App-e2689782. You can also use the Share Source Sample app as an example and leverage code from this app; you can download it from http://code.msdn.microsoft.com/windowsapps/Sharing-Content-Source-App-d9bffd84.
Hope that helps!
Ok, perhaps the preview version of the Mail app doesn't handle the sharing target contract correctly. Using the SDK sample app "Sharing Content Target App" from http://code.msdn.microsoft.com/windowsapps/Sharing-Content-Target-App-e2689782, sharing a StorageItem created in memory with the StorageFile.CreateStreamedFileAsync() method posted above works fine.
Thus, that's the way you should go when you want to share in-memory byte[] arrays. For testing, make sure that the share target app doesn't run in Visual Studio when you want to share data from another app with it. Then the sharing sidebar mysteriously will disappear automatically...

Plugin.ImageResourceName doesn't seem to have any effect

It would be great if the Petrel Plugin Manager could display our custom bitmap for each of our plugins - however, the Plugin.ImageResourceName property doesn't seem to have any effect.
public override string ImageResourceName { get { return "Blueback.Toolbox.Plugin.Toolbox.png"; } }
The image is embedded correctly (according to the documentation and ILDisAsm) - but Plugin Manager insists on using the generic image instead. Are there undocumented requirements on dimensions or format? The code snippets in the documentation mention both bmp and png, without demonstrating that the property actually works.
I haven't been able to locate an actual running sample in the SDK (only Module samples) nor in the code sample downloads (several Plugins here, but they return null for the resource name).
Can anyone provide a working sample or the missing key?
The image provided via Plugin.ImageResourceName is displayed in the Petrel License Dialog, and you are right, it is not displayed in Plugin Manager as it always uses the generic image to represent plugins. We will consider changing it in Petrel 2013.1.