Showing posts with label Articles. Show all posts
Showing posts with label Articles. Show all posts

Thursday, March 9, 2017

JPG Image size reducer. How to reduce jpg to desired size without changing resolution on PC and Android?

When we click a photo it contains lot of colors which our eyes cannot differentiate. Higher colors means higher image size and with evolving camera chips images are getting bigger and bigger in size. There are many methods to reduce photo size. Lets look at some methods of reducing image size.


I recently backup my images on Google drive. The images were bigger in size and were in GBs in size which i reduced to ~700MB (deleted few repeated images). Among size reduction methods most of us reduce the image resolution i.e. number of pixels are reduced. For example Full HD images are reduced to qHD or 720p. But i didn't reduced the number of pixels but number of colors!


This is already a reduced image of size ~900KB. I captured this image in my garden in 2016 summer using my Nubia Z9 mini phone. Its already reduced in size to load faster on website.



Original: 942KB


 Half resolution: 711KB





Reduced Colors: 268KB



Looking at images its clear that reducing number of colors has greater size reduction than reducing resolution only. Do check the images by downloading them and zoom. Decide yourself which one is better!


Now coming to the tools used for size reduction. Without taking much time below are some tools to reduce size of jpg.

  • ImageMagick's convert tool. This s a commandline tool which can reduce jpg size to desired size. Example: convert -define jpeg:extent=500kb nubia_bks_camera.jpg out.jpg. This will reduce original image to ~500KB without changing resolution.
  • jpegoptim: this tool is same as imagemagick with desired jpg size.
  • IrfanView. This is a windows only GUI software which can reduce jpg files to desired size.

I have created my own Android app which can do above size reduction and is very easy to work with. Here is the link to my app Jpg Image Size Reducer: https://play.google.com/store/apps/details?id=org.greh.imagesizereducer. A quick usage example of this app is you share your selfie which is 2MB and want to reduce size quickly without editing the image. Share it with any image app and select jpg image size reducer and reduce to whatever % or KB and then share the copy to wherever you want!. It can also reduce many files in batch. Its also an image converter and can convert non-jpg files to jpg and reduce its size.



The software above reduce number of colors which reduces size of image even more than reducing size does. However Using both methods can give very small size images. Reducing total colors to few thousand or hundreds can be noticed easily by zooming. Another method to reduce image size is to change image mode to INDEXED from RGB. Indexed mode means all colors are collected at a place/table and index/position of a color from table is used. This can reduce size. However this is not always better.

Friday, October 14, 2016

Audio Visualizer using libGDX for multiple platforms

This article & project is about visualizing audio as waveforms on screen. Before starting let's understand few terms and minimum requirements related to the project. Most of details about app is inside the source code. This article is only about audio section.

Requirements:
  • A device supported by libGDX library. 
  • Device must support microphone. 



Sample Rate:
Total number of samples of audio to be captured per second. We will use standard 44100 samples per second i.e. 44.1KHz.


Audio Sample:
Size of 1 sample. 1 sample is signed 16 bits in size and therefore can represent total 65536 volume levels. Negative samples have value from -1 to -32768 and +ve samples have value from 0 to 32767. We will use Mono samples only. Please refer to Audio documentation on Wikipedia, website or a book for further information on samplerate etc.



Working:
This project uses libGDX audio recorder interface to capture audio from MIC. Code is extremely simple to understand and implement on multiple platforms. Functions to create & delete recorder.

public final void make_recorder() {
        if( m_rec == null ) {
            m_rec = Gdx.audio.newAudioRecorder(44100, true);
            m_samples = new short[ (int)(44100f / FPS)];            
        }
    }
    public void destroy_recorder() {
        if( m_rec != null ) {
            m_rec.dispose();
            m_rec = null;
        }
    }

Below code is used for capturing audio samples in a buffer for drawing purpose.

m_rec.read(m_samples, 0, m_samples.length);
// plot samples by moving from left middle of screen to right middle. Delta is how
// high the resolution of plotting is.
for( float i = 0; i <= 100f; i += delta ) {
       
}




m_sample is a short buffer of size 44100/FPS. This is because in this project we draw screen at 25FPS and every frame plots audio data. So we divide 1 second of audio into 25 parts and sync with screen. Total samples we need per frame is 44100/FPS.



Pseudo code of drawing audio data:
  • Capture 44100/FPS number of samples into SAMPLES buffer. 
  • Loop from left middle of screen i.e. x=0 to x=100% of screen width plotting audio sample related to the % value. Means x=0% means 0% position of sample in SAMPLES. x=35% = 35/100 x SAMPLES's SIZE. Formula: Audio Sample = PERCENT_POS/100 * SAMPLES's SIZE. 
  • Take amplitude of sample as per your choice and plot audio using pixel. Plot line between current and last sample. 


Visualizer snapshot:



Download link for java only source code of project.

MylibGDX-articles-14-Oct-2016.7z


With same logic i built my Sound Visualizer which renders beautiful design using voices. It can be called Rangoli Maker or simple Mandalas or Cymatics kind of plotter. Cymatics needs lots of calculation in 3D but this app is good for flower like designs.


Download link for Android app:
http://play.google.com/store/apps/details?id=org.greh.sacredsound