This is notoriously tricky with batteries. The voltage - being more or less constant during the discharge cycle, except that it varies with temperature and current drawn, as well as with age - is not a good indication. You can measure the energy in and the energy out and this gives you some idea, but as batteries age this becomes unreliable too, and again temperature comes into it. With smart software and temperature sensors etc. a microprocessor can improve the accuracy quite a bit but it remains a rather rough and ready indictor.
Capacitors are much easier. The energy remaining (the 'gas' in the tank) is directly proportional to the voltage squared. So you just measure the voltage and square it if you want a nice linear indication. This is easy to do with a microprocesor, or with an analogue 2 (or 4) quadrant multiplier chip and you can have the indication in Joules, Watt-Hours - whatever you want.