Substance discrimination beyond the shape feature is urgently desired for x-ray imaging for enhancing target identification. With two x-ray sources or stacked two detectors, the two-energy-channel x-ray detection can discriminate substance density by normalizing the target thickness. Nevertheless, the artifacts, high radiation dose and difficulty in image alignment due to two sources or two detectors impede their widespread application. In this work, we report a single direct x-ray detector with MAPbI3/MAPbBr3 heterojunction for switchable soft x-ray (<20 keV) and hard x-ray (>20 keV) detection under one x-ray source. Systematic characterizations confirm soft and hard x-ray deposit their energy in MAPbI3 and MAPbBr3 layer, respectively, while working voltages can control the collection of generated charge carriers in each layer for selective soft/hard x-ray detection. The switching rate between soft and hard x-ray detection mode reaches 100 Hz. Moreover, the detector possesses a moderate performance with ~50 nGy s−1 in limit-of-detection, ~8000 μC Gy−1 cm−2 in sensitivity and ~7 lp/mm in imaging resolution. By defining the attenuation coefficient ratio (μL/μH) as substance label, we effectively mitigate the influence of target thickness and successfully discriminate substances in the acquired x-ray images. (Figure presented.).