Saturday, May 31, 2025

[JAVA] CAMERA

import com.github.sarxos.webcam.Webcam;
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.io.IOException;

    public class CameraExample {
        public static void main(String[] args) throws IOException {
            Webcam webcam = Webcam.getDefault();
            webcam.open();
            ImageIO.write(webcam.getImage(), "PNG", new File("capture.png"));
            webcam.close();
        }
    }

[JAVA] VOLUME AUDIO

import javax.sound.sampled.*;

public class VolumeControl {
    public static void main(String[] args) {
        try {
            // Load an audio file
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(VolumeControl.class.getResource("audio.wav"));
            // Get a clip
            Clip clip = AudioSystem.getClip();
            // Open the clip
            clip.open(audioInputStream);
            // Get the master gain control
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);

            // Set the volume (example: 50% of the maximum)
            float maxGain = gainControl.getMaximum();
            float minGain = gainControl.getMinimum();
            float range = maxGain - minGain;
            float volume = minGain + (range * 0.5f); // 50%
            gainControl.setValue(volume);

            // Start playing
            clip.start();

            // Keep the program running while the audio plays
            Thread.sleep(clip.getMicrosecondLength() / 1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

[JAVA] PROVIDER

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

[JAVA] PROXY

   Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy_host_name", proxy_port_number));
    URL url = new URL("your_url");
    URLConnection urlConnection = url.openConnection(proxy);

[JAVA] READ HEX from File

import java.io.FileInputStream;
import java.io.IOException;

public class HexFileReader {

    public static byte[] readFileBytes(String filePath) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        byte[] fileBytes = fis.readAllBytes();
        fis.close();
        return fileBytes;
    }

    public static String bytesToHex(byte[] bytes) {
      StringBuilder hexString = new StringBuilder();
      for (byte b : bytes) {
          hexString.append(String.format("%02x", b & 0xFF));
      }
      return hexString.toString();
    }

    public static void main(String[] args) {
        String filePath = "path/to/your/file.txt"; // Replace with your file path
        try {
            byte[] fileBytes = readFileBytes(filePath);
            String hexString = bytesToHex(fileBytes);
            System.out.println("Hexadecimal representation:\n" + hexString);
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
        }
    }
}

[JAVA] HEX from File

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.commons.codec.binary.Hex;

public class HexFileUtil {

    public static void main(String[] args) {
        String filePath = "path/to/your/file.txt";

        try {
            // Example 1: Read and convert hex string to byte array
            String hexString = Files.readString(Paths.get(filePath)).trim();
            byte[] byteArray = hexStringToByteArray(hexString);
            System.out.println("Byte array from hex string: " + java.util.Arrays.toString(byteArray));

            // Example 2: Read file as bytes and convert to hex string
            byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
            String hexStringFromFile = byteArrayToHexString(fileBytes);
            System.out.println("Hex string from file bytes: " + hexStringFromFile);


             // Example 3: Read file as bytes and convert to hex string using Apache Commons Codec
             byte[] fileBytes2 = Files.readAllBytes(Paths.get(filePath));
             String hexStringFromBytes = Hex.encodeHexString(fileBytes2);
             System.out.println("Hex string from file bytes using Apache Commons Codec: " + hexStringFromBytes);


            // Example 4: Read file and print it in hex representation using FileInputStream

            printHexStream(new FileInputStream(filePath), 16);

        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
        }
    }


    public static void printHexStream(final FileInputStream inputStream, final int numberOfColumns) throws IOException{
         long streamPtr=0;
         while (inputStream.available() > 0) {
             final long col = streamPtr++ % numberOfColumns;
             System.out.printf("%02x ",inputStream.read());
             if (col == (numberOfColumns-1)) {
                 System.out.printf("\n");
             }
         }
    }

    public static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                                 + Character.digit(hexString.charAt(i + 1), 16));
        }
        return data;
    }

    public static String byteArrayToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}

[JAVA] PRINT DATA

import java.awt.*;
import java.awt.print.*;

public class PrintExample implements Printable {
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.drawString("Hello, Printer!", 100, 100);
        return PAGE_EXISTS;
    }

    public static void main(String[] args) {
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        PrintService[] services = PrinterJob.lookupPrintServices();
        if (services.length > 0) {
            try {
                printerJob.setPrintService(services[0]);
                printerJob.setPrintable(new PrintExample());
                if (printerJob.printDialog()) {
                    printerJob.print();
                }
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("No printers found.");
        }
    }
}